【问题标题】:ComboBox does not display existing bound dataComboBox 不显示现有的绑定数据
【发布时间】:2013-03-21 04:16:31
【问题描述】:

我在 DataGrid 中定义了一个 ComboBox,如图所示。 ComboBoxItemsSource 是自定义类CodePickList 的列表,其中包含NameID 属性。 DisplayMemberPathName 属性,SelectedValuePathID 属性。 SelectedValue 绑定到 DataGrid ItemsSource 上的 GLCode 属性。 (请注意,DataGridItemsSource 是一个视图。)ComboBox 正确填充,并且 ComboBox 选择绑定到 @ 987654334@ 属性也可以。 数据一切正常。问题在于 DataGrid 的ItemsSource 中的现有数据。 ComboBox 的文本块只是空白。显然 ComboBox 无法将 DataGrid ItemsSource 上的 GLCode 属性与 ComboBox @987654339 上的 ID 属性相匹配@ ComboBox 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>

【问题讨论】:

  • 不需要像这样制作单元格模板,可以使用DataGridComboBoxColum,参考这篇文章stackoverflow.com/questions/5409259/…
  • @Hannish,谢谢你,我已经看到你提到的帖子了。您对 CellTemplate 是正确的,但显示问题仍然存在。其他帖子让我相信这是一个时间问题,这意味着 SelectValue 以某种方式对 ComboBox 进行评估过早或过晚。到目前为止还没有运气。

标签: .net wpf xaml combobox


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-06-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2011-04-07
    • 1970-01-01
    • 2014-03-31
    • 2021-03-09
    相关资源
    最近更新 更多