0

我的目标是在 a 中放入ComboBox可编辑的内容DataGrid,为此我放入了 a TextBlockinCellTemplate和 a ComboBoxin 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;
4

1 回答 1

0

在这种特殊情况下,您将不得不在支持 DataGridItem 的模型中再创建一个属性,例如 SelectedBinding。属性的类型应该是 BindingList 中的项目类型。

然后你可以将你的 ComboBox 的 SelectedItem 绑定到它

comboBoxFactory.SetValue(ComboBox.SelectedItemProperty, new Binding("SelectedBinding"));

您可以将 TextBlock 绑定更新为

  dgc.ClipboardContentBinding = new Binding("SelectedBinding.Description");
于 2013-10-24T13:57:02.123 回答