我正在以編程方式創建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,這正是我的'普通'列所發生的情況)。