ComboBox不显示现有绑定数据

时间:2013-03-30 16:05:43

标签: .net wpf xaml combobox

我在 DataGrid 中定义了 ComboBox ,如图所示。 ComboBox ItemsSource是包含CodePickListName属性的自定义类ID的列表。 DisplayMemberPathName属性,SelectedValuePathID属性。 SelectedValue绑定到 DataGrid GLCode上的ItemsSource属性。 (请注意, DataGrid ItemsSource是一个视图。) ComboBox 正确填充并将 ComboBox 选项绑定到GLCode属性也可以。对于数据都很好。问题在于 DataGrid的ItemsSource中的现有数据。 ComboBox的文本块只是空白。显然, ComboBox 无法匹配 DataGrid GLCode上的ItemsSource属性与 ComboBox上的ID属性加载ComboBox ItemsSource时 ItemsSource。这些属性属于同一类型。 WinForms处理这种情况,但我不能让它在WPF中工作。

<DataGrid ItemsSource="{Binding EntityDetailsView.View}">
    <DataGrid.Columns>
        <DataGridTemplateColumn>
             <DataGridTemplateColumn.CellTemplate>
                 <DataTemplate>
                        <ComboBox ItemsSource="{BindingPath=DataContext.CodePickList, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                  DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValue="{Binding GLCode, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                                  IsEditable="False" IsReadOnly="False"/>
                 </DataTemplate> 
             </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

1 个答案:

答案 0 :(得分:0)

事实证明,DataGrid的ItemsSource中的GLCode来自数据库(通过实体框架)作为固定长度的字符串(填充右侧)。 ComboBox的ItemsSource中的ID是普通的C#字符串。因此,值不匹配,并且不会显示现有数据。解决方案是填充ComboBox ItemsSource中的ID字符串。 ComboBox现在按预期工作。