2013-11-27 107 views
5

我正在以編程方式創建DataGrid,並且也必須支持ComboBoxColumns將DataGridComboBoxColumn綁定到C#中的DataGrid的ItemsSource

在我創建DataGrid後,我將其設置爲ItemSource以收集BindableList<BindableDictionary>類型的集合。 BindableDictionary是一種自定義類型。每個BindableDictionary代表一個元組。關鍵始終是列的名稱,它的值是一個自定義類,它包含一個名爲ActualValue的通用屬性,一個名爲AllowedValuesDictionary<T, string>和一個boolean,用於確定AllowedValues是否將用於構建ComboBoxColumn或「正常」列。該類還實現了INotifyPropertyChangedINotifyPropertyChanging

除了ComboBoxColumn外,這些東西還可以工作。我與ComboBoxColumn問題是,我不知道如何得到它使用AllowedValues對象來填補它的ITEMLIST 使用ActualValue屬性選擇從AllowedValuesBindableDictionary正確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) } }); 

是的,這工作。

我試圖DataGridComboBoxColumnItemsSource屬性設置爲column.AllowedValues,並設置DisplayPath"Value"這也至少顯示正確的內容,但我不知道如何綁定到包含在DataGridActualValue財產的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,這正是我的'普通'列所發生的情況)。

回答

0

我做了類似的事情。我正在使用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列)之後,我會先手動添加組合框的值,然後再那些被添加,我會檢查組合框的值並將其設置爲。

我還在數據錯誤事件中包含了這個函數,當我添加一個新列時。如果我添加了一個新行,如果我設置了autosize,它也會隨機崩潰。我必須將這些設置爲默認值,進行編輯,然後重置它。

希望我有代碼提供,但它可能會讓你在你的路上。如果不是當我明天上班時,我會張貼一些。 Comboboxes是一個巨大的工作痛苦。

相關問題