[英]WPF change datagrid combobox ItemsSource to value of another datagrid combox
[英]WPF Datagrid Combox editable
我的目標是將一個可編輯的ComboBox
放入DataGrid
,為此我將TextBlock
放入CellTemplate
並將ComboBox
放入CellEditingTemplate
。 但是,如何將我的TextBlock
與ComboBox
中所選項目的文本綁定?
var textBlockFactory = new FrameworkElementFactory(typeof(TextBlock));
textBlockFactory.SetBinding(TextBlock.TextProperty, new Binding("Description"));
dgc.ClipboardContentBinding = new Binding("Description");
var template = new DataTemplate();
template.VisualTree = textBlockFactory;
dgc.CellTemplate = template;
var comboBoxFactory = new FrameworkElementFactory(typeof(ComboBox));
template = new DataTemplate();
template.VisualTree = comboBoxFactory;
Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(UserControl), 1);
b.Path = new PropertyPath(BindingList);
comboBoxFactory.SetBinding(ComboBox.ItemsSourceProperty, b);
comboBoxFactory.SetValue(ComboBox.IsEditableProperty, true);
comboBoxFactory.SetValue(ComboBox.SelectedValueProperty, new Binding("Type"));
comboBoxFactory.SetValue(ComboBox.SelectedValuePathProperty, "Id");
comboBoxFactory.SetValue(ComboBox.DisplayMemberPathProperty, "Description");
dgc.CellEditingTemplate = template;
dgc.SortMemberPath = BindingCurrentItem;
在這種特定情況下,您將必須在支持DataGridItem的模型中再增加一個屬性,例如SelectedBinding。 屬性的類型應該是BindingList中項目的類型。
然后您可以將ComboBox的SelectedItem綁定為
comboBoxFactory.SetValue(ComboBox.SelectedItemProperty, new Binding("SelectedBinding"));
您可以將TextBlock綁定更新為
dgc.ClipboardContentBinding = new Binding("SelectedBinding.Description");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.