DataGrid
をプログラムで作成しており、ComboBoxColumns
もサポートする必要があります。DataGridComboBoxColumnをDataGridのItemsSourceにバインドするC#
DataGrid
を作成した後、ItemSource
をBindableList<BindableDictionary>
のコレクションに設定しました。 BindableDictionary
はカスタムタイプです。各BindableDictionary
は1つのタプルを表します。キーは常に列の名前であり、その値はActualValue
という012,というAllowedValues
とboolean
と呼ばれる総称プロパティを含み、ComboBoxColumn
または '通常の'列を構築するためにAllowedValues
が使用されるかどうかを決定するカスタムクラスです。また、そのクラスはINotifyPropertyChanged
とINotifyPropertyChanging
を実装しています。
ComboBoxColumnを除いて、そのことがうまく機能します。 ComboBoxColumnと私の問題は、私はそれはそれはITEMLIST だ埋めるためにAllowedValues
オブジェクトを使用して取得し、テキストエリアを埋めるためにAllowedValues
BindableDictionary
から正しいValue
を選択するために、ActualValue
プロパティを使用する方法がわからないということです。
例として、これは私がtextbased列バインドする方法です:
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) } });
そして、はい、動作しますが。
は私がcolumn.AllowedValues
に
DataGridComboBoxColumn
の
ItemsSource
プロパティを設定し、
DisplayPath
少なくとも正しいコンテンツを表示しない
"Value"
に設定しようとしたが、私はどのように
DataGrid
に含まれている
ActualValue
プロパティにバインドする手掛かりをしたん's
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にはBindableDictionaryが含まれているので、これはBindingList [iterator] ["MyColumn"]に解決する必要があります。