我正在以编程方式创建DataGrid
,并且也必须支持ComboBoxColumns
。将DataGridComboBoxColumn绑定到C#中的DataGrid的ItemsSource
在我创建DataGrid
后,我将其设置为ItemSource
以收集BindableList<BindableDictionary>
类型的集合。 BindableDictionary
是一种自定义类型。每个BindableDictionary
代表一个元组。关键始终是列的名称,它的值是一个自定义类,它包含一个名为ActualValue
的通用属性,一个名为AllowedValues
的Dictionary<T, string>
和一个boolean
,用于确定AllowedValues
是否将用于构建ComboBoxColumn
或“正常”列。该类还实现了INotifyPropertyChanged
和INotifyPropertyChanging
。
除了ComboBoxColumn外,这些东西还可以工作。我与ComboBoxColumn问题是,我不知道如何得到它使用AllowedValues
对象来填补它的ITEMLIST 和使用ActualValue
属性选择从AllowedValues
BindableDictionary
正确Value
填补文本区域。
举个例子,我这是怎么绑定一个基于文本的列:
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) } });
是的,这工作。
我试图DataGridComboBoxColumn
的ItemsSource
属性设置为column.AllowedValues
,并设置DisplayPath
到"Value"
这也至少显示正确的内容,但我不知道如何绑定到包含在DataGrid
的ActualValue
财产的ItemsSource
。此外,这意味着列内的所有单元共享相同的可选值,这可能会导致未来出现问题。
如果我试图像DataGridTextColumn
那样绑定所有东西,则根本不会显示任何东西。也没有可供选择的项目。
如果有人有我可以尝试的暗示,那将会很棒。
编辑
刚看到这一点:https://stackoverflow.com/a/2197004/937093,我想,但后来我得到了我的输出窗口中出现以下消息:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=AllowedValues; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=33493530); target property is 'ItemsSource' (type '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/
我尝试申请代理绑定的东西(虽然我不明白的列是如何在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 Error: 40 : BindingExpression path error: 'MyColumn' property not found on 'object' ''BindingList`1' (HashCode=55207835)'. BindingExpression:Path=Data.MyColumn.ActualValue; DataItem='BindingProxy' (HashCode=45660050); target element is 'TextBlockComboBox' (Name=''); target property is 'SelectedValue' (type 'Object')
我理解这个问题(它试图发现在的BindingList的“MyColumn”对象),但我不明白这是为什么发生的事情(它应该解析为BindingList [iterator] [“MyColumn”],因为BindingList包含BindableDictionary,这正是我的'普通'列所发生的情况)。