通过C#代码创建DataGridTemplateColumn

Eri*_* R. 15 c# wpf datagrid combobox textblock

我有一个我创建的动态Datagrid.我通过后面的代码为它创建每一列.我想在不编辑时在文本块上显示的列上遇到麻烦,但在编辑时作为组合框显示.我有一个ObservableCollection of Transactions.每个交易都有一个名为"账户"的类型.这是我到目前为止:

    private DataGridTemplateColumn GetAccountColumn()
    {
        // Create The Column
        DataGridTemplateColumn accountColumn = new DataGridTemplateColumn();
        accountColumn.Header = "Account";

        Binding bind = new Binding("Account");
        bind.Mode = BindingMode.TwoWay;

        // Create the TextBlock
        FrameworkElementFactory textFactory = new FrameworkElementFactory(typeof(TextBlock));
        textFactory.SetBinding(TextBlock.TextProperty, bind);
        DataTemplate textTemplate = new DataTemplate();
        textTemplate.VisualTree = textFactory;

        // Create the ComboBox
        bind.Mode = BindingMode.OneWay;
        FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox));
        comboFactory.SetValue(ComboBox.DataContextProperty, this.Transactions);
        comboFactory.SetValue(ComboBox.IsTextSearchEnabledProperty, true);
        comboFactory.SetBinding(ComboBox.ItemsSourceProperty, bind);

        DataTemplate comboTemplate = new DataTemplate();
        comboTemplate.VisualTree = comboFactory;

        // Set the Templates to the Column
        accountColumn.CellTemplate = textTemplate;
        accountColumn.CellEditingTemplate = comboTemplate;

        return accountColumn;
    }
Run Code Online (Sandbox Code Playgroud)

该值显示在TextBlock中.但是,在组合框中,每个项目只显示一个字符.例如,这是文本块:

在此输入图像描述

但是当我点击编辑并进入组合框时,这里显示的是:

在此输入图像描述

有人可以帮助我,以便Combobox中的项目正确显示?此外,当我从Combobox中选择某些内容时,文本块不会使用我选择的项目进行更新.

更新:

这是我现在的专栏.ComboBox中的项目正在正确显示.现在的问题是,当选择新项目时,TextBlock中的文本不会使用新项目进行更新.

    private DataGridTemplateColumn GetAccountColumn()
    {
        // Create The Column
        DataGridTemplateColumn accountColumn = new DataGridTemplateColumn();
        accountColumn.Header = "Account";

        Binding bind = new Binding("Account");
        bind.Mode = BindingMode.OneWay;

        // Create the TextBlock
        FrameworkElementFactory textFactory = new FrameworkElementFactory(typeof(TextBlock));
        textFactory.SetBinding(TextBlock.TextProperty, bind);
        DataTemplate textTemplate = new DataTemplate();
        textTemplate.VisualTree = textFactory;

        // Create the ComboBox
        Binding comboBind = new Binding("Account");
        comboBind.Mode = BindingMode.OneWay;

        FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox));
        comboFactory.SetValue(ComboBox.IsTextSearchEnabledProperty, true);
        comboFactory.SetValue(ComboBox.ItemsSourceProperty, this.Accounts);
        comboFactory.SetBinding(ComboBox.SelectedItemProperty, comboBind);

        DataTemplate comboTemplate = new DataTemplate();
        comboTemplate.VisualTree = comboFactory;

        // Set the Templates to the Column
        accountColumn.CellTemplate = textTemplate;
        accountColumn.CellEditingTemplate = comboTemplate;

        return accountColumn;
    }
Run Code Online (Sandbox Code Playgroud)

"Accounts"属性在我的MainWindow类中声明如下:

public ObservableCollection<string> Accounts { get; set; }

    public MainWindow()
    {
        this.Types = new ObservableCollection<string>();
        this.Parents = new ObservableCollection<string>();
        this.Transactions = new ObservableCollection<Transaction>();
        this.Accounts = new ObservableCollection<string>();

        OpenDatabase();
        InitializeComponent();
    }
Run Code Online (Sandbox Code Playgroud)

这是我的交易类:

public class Transaction
{
    private string date;
    private string number;
    private string account;

    public string Date
    {
        get { return date; }
        set { date = value; }
    }

    public string Number
    {
        get { return number; }
        set { number = value; }
    }

    public string Account
    {
        get { return account; }
        set { account = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

H.B*_*.B. 4

您将 绑定ItemsSource到选定的值(一个字符串,又名字符数组),因此每个字符都用作一个项目,绑定ItemsSource可能应该针对可以从中选择值的其他集合。