【问题标题】:Binding a DataGridComboBoxColumn to the DataGrid's ItemsSource in C#在 C# 中将 DataGridComboBoxColumn 绑定到 DataGrid 的 ItemsSource
【发布时间】:2013-12-12 19:45:40
【问题描述】:

我正在以编程方式创建DataGrid,并且还必须支持ComboBoxColumns

在创建DataGrid 之后,我将它的ItemSource 设置为BindableList<BindableDictionary> 类型的集合的集合。 BindableDictionary 是自定义类型。每个BindableDictionary 代表一个元组。它的键始终是列的名称,它的值是一个自定义类,其中包含一个名为ActualValue 的通用属性、一个名为AllowedValuesDictionary<T, string> 和一个确定是否使用AllowedValues 构建的boolean ComboBoxColumn 或“正常”列。该类还实现了INotifyPropertyChangedINotifyPropertyChanging

除了 ComboBoxColumn 之外,这些东西都有效,呵呵。我对 ComboBoxColumn 的问题是我不知道如何让它使用 AllowedValues 对象来填充它的 ItemList 并且使用 ActualValue 属性从AllowedValuesBindableDictionary 填充文本区域。

例如,这是我绑定基于文本的列的方式:

table.Columns.Add(new DataGridTextColumn() { Header = column.GUIName, DisplayIndex = column.Position, Binding = new Binding(column.Name + ".ActualValue") { UpdateSourceTrigger = UpdateSourceTrigger.Default, Mode = BindingMode.TwoWay, NotifyOnTargetUpdated = true, NotifyOnSourceUpdated = true, UpdateSourceExceptionFilter = new UpdateSourceExceptionFilterCallback(BindingExceptionHandler) } });

是的,这行得通。

我尝试将DataGridComboBoxColumnItemsSource 属性设置为column.AllowedValues,并将DisplayPath 设置为"Value",这至少可以显示正确的内容,但我不知道如何绑定到DataGridItemsSource 中包含的 ActualValue 属性。这也意味着一列中的所有单元格共享相同的可选值,这可能会导致将来出现问题。

如果我尝试像在DataGridTextColumn 中那样绑定所有内容,则根本不会显示任何内容。也没有可供选择的项目。

如果有人能暗示我可以尝试什么,那就太棒了。

编辑

刚看到这个:https://stackoverflow.com/a/2197004/937093,我试过了,但我在输出窗口中收到以下消息:

System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。绑定表达式:路径=允许值;数据项=空;目标元素是“DataGridComboBoxColumn”(HashCode=33493530);目标属性是“ItemsSource”(类型“IEnumerable”)

我的代码如下所示:

col = new DataGridComboBoxColumn() { Header = column.GUIName, SelectedValueBinding = new Binding(column.Name + ".ActualValue"), SelectedValuePath = "ActualValue" };
table.Columns.Add(col);
BindingOperations.SetBinding(col, DataGridComboBoxColumn.ItemsSourceProperty, new Binding("AllowedValues"));

编辑 2 好的,找到这个网站:http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

我尝试应用代理绑定的东西(即使我不明白 Column 是如何不是 DataGrid 的可视化树的一部分......它还能在哪里?!)但它不会工作。我的代码:

BindingProxy proxy = new BindingProxy() { Data = table.ItemsSource };
table.Resources.Add("proxy", proxy });
col = new DataGridComboBoxColumn() { Header = column.GUIName, SelectedValueBinding = new Binding("Data." + column.Name + ".ActualValue") { Source = proxy }, DisplayMemberPath = "Value", SelectedValuePath = "Key" };
table.Columns.Add(col);
BindingOperations.SetBinding(col, DataGridComboBoxColumn.ItemsSourceProperty, new Binding("Data." + column.Name + ".AllowedValues") });

在输出窗口中输出:

System.Windows.Data 错误:40:BindingExpression 路径错误:“MyColumn” 在“对象”“BindingList`1”(HashCode=55207835)上找不到属性。 BindingExpression:Path=Data.MyColumn.ActualValue; DataItem='BindingProxy' (HashCode=45660050);目标元素是 'TextBlockComboBox'(名称='');目标属性是“SelectedValue” (类型“对象”)

我了解问题(它试图在 BindingList 中找到“MyColumn”对象)但我不明白为什么会发生这种情况(它应该解析为 BindingList[iterator]["MyColumn"],因为 BindingList 包含BindableDictionary,这正是我的“普通”列发生的情况)。

【问题讨论】:

    标签: c# .net wpf datagrid wpfdatagrid


    【解决方案1】:

    我做了类似的事情。我使用的是 Winforms,所以我的解决方案可能不适合你。不过我建议使用类似的东西。

    http://tech.pro/tutorial/776/csharp-tutorial-binding-a-datagridview-to-a-collection

    加上这个。

    http://www.codeproject.com/Articles/31418/Implementing-a-Sortable-BindingList-Very-Very-Quic

    因为我一切正常,然后发现我无法对我的 datagridview 进行排序。

    我手头没有我的源代码,但总体思路是我会手动创建组合框和文本框列,然后将我的列表绑定到它。

    在我运行一个基于索引遍历每一行的函数之后(在我的情况下,我的组合框是最后 3 列)我会先手动添加组合框的值,然后在这些值之后添加后,我会检查组合框的值并将其设置为该值。

    我还在数据错误事件中包含了这个函数,当我添加一个新列时。如果我添加了一个新行,如果我设置了自动大小,它也会随机崩溃。我必须将这些设置为默认值,进行编辑,然后重置它。

    希望我能提供代码,但它可能会让您上路。如果不是明天我上班的时候我会发布一些。组合框很难使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 2015-01-21
      • 2014-01-04
      • 2015-11-22
      • 2011-03-12
      • 2023-03-07
      • 1970-01-01
      相关资源
      最近更新 更多