XAML DataGridComboBoxColumn数据绑定

bvn4nwqk  于 11个月前  发布在  其他
关注(0)|答案(5)|浏览(152)

我正在尝试数据绑定DataGridComboBoxColumn

<DataGridComboBoxColumn Header="Number of Copies" SelectedItemBinding="{Binding NumberCopies}">
    <DataGridComboBoxColumn.ElementStyle>
       <Style TargetType="ComboBox">
          <Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
          <Setter Property="IsReadOnly" Value="True"/>
       </Style>
    </DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>

字符串
我在这里做错了什么,因为我在运行时得到了一个空的组合框。
有人跟踪我
系统.Windows.数据错误:第二章:找不到目标元素的控制FrameworkElement或FrameworkContentElement。BindingExpression:Path=LifeAreaList; DataItem=null;目标元素为“DataGridComboBoxColumn”(HashCode=49475561);目标属性为“ItemsSource”(类型为“IEnumerable”)

yvgpqqbh

yvgpqqbh1#

DataGridColumn不是从FrameworkElementFrameworkContentElement派生的,所以它不在可视树中,也没有DataContext,这就是为什么你的绑定失败。
如果你绑定到的List<int>对每个项目都是一样的,那么也许你应该找到另一种方法来绑定它,也许你可以让它静态化,并在绑定中使用StaticResource
无论如何,要将ItemsSource绑定到源类中的List<int>属性,您可以使用ElementStyleElementEditingStyle(正如其他人所指出的那样)。

<DataGridComboBoxColumn Header="Number of Copies"
                        SelectedItemBinding="{Binding ListAreaItem}">
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

字符串

tgabmvqs

tgabmvqs2#

你不应该在样式中设置ItemsSource,列本身has such a property可能会覆盖你试图在样式中设置的任何东西。此外,你试图将其设置为错误的样式(该样式用于显示模式),你可以尝试在EditingElementStyle中设置它,但我也不建议这样做。

wb1gzix0

wb1gzix03#

为什么要在样式中设置项目源?
你可以试试这个代码:

<my:DataGridTemplateColumn Header="Number of Copies" >
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=LifeAreaList}"  >
                               <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding .}"></Label>
                                </DataTemplate>
                               </ComboBox.ItemTemplate>
                            </ComboBox>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>

字符串
如果LifeAreaList是复杂的类集合,并且您希望以自定义的方式显示它,则为DataGridTemplateColumn定义数据模板。

xvw2m8pv

xvw2m8pv4#

我会尝试使用PresentationTraceSources.TraceLevel=“High”的常规DataGridColumn,看看是否存在绑定问题。

fnx2tebb

fnx2tebb5#

如果你想使用类似于DataGridCobmoboxColumn的东西,最好使用DataGridTemplateColumn,你可以使用它,并具有相同的功能,而不需要使用ItemsSourceStaticResource
我将RelativeSource中的根元素设置为UserControl,如果您的DataGrid位于WindowPage中,则需要相应地使用它们。

<DataGridTemplateColumn Header="Number of Copies">
          <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                  <TextBlock Text="{Binding NumberCopies}"/>
              </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
          <DataGridTemplateColumn.CellEditingTemplate>
              <DataTemplate>
                  <ComboBox SelectedItem="{Binding NumberCopies, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding DataContext.LifeAreaList, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>
                  </DataTemplate>
              </DataGridTemplateColumn.CellEditingTemplate>
          </DataGridTemplateColumn>

字符串

相关问题