0

私の目標は、ComboBox編集可能なものを aに入れることです。そのために、 aと aをDataGrid入れます。しかし、どのように私を選択したアイテムのテキストにバインドしますか?TextBlockCellTemplateComboBoxCellEditingTemplateTextBlockComboBox

        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 というプロパティをもう 1 つ作成する必要があります。プロパティのタイプは、BindingList 内のアイテムのタイプである必要があります。

次に、ComboBox の SelectedItem を次のようにバインドできます。

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

TextBlockバインディングを次のように更新できます

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