通过C#代码创建DataGridTemplateColumn

时间:2012-01-08 18:06:02

标签: 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;
    }

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

enter image description here

但是当我点击编辑并进入组合框时,显示的内容如下:

enter image description here

有人可以帮助我,以便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;
    }

在我的MainWindow类中,“Accounts”属性声明如下:

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();
    }

这是我的交易类:

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; }
    }
}

2 个答案:

答案 0 :(得分:4)

您将ItemsSource绑定到选定的值,一个字符串,也就是char数组,因此每个字符都用作一个项目,ItemsSource绑定可能应该定位一个其他集合,其中值可以被选中。

答案 1 :(得分:1)

Dim newBind As Binding = New Binding("LinktoCommonOutputBus")
newBind.Mode = BindingMode.OneWay

factory1.SetValue(ComboBox.ItemsSourceProperty, dictionary)
factory1.SetValue(ComboBox.NameProperty, name)
factory1.SetValue(ComboBox.SelectedValuePathProperty, "Key")
factory1.SetValue(ComboBox.DisplayMemberPathProperty, "Value")
factory1.SetBinding(ComboBox.SelectedValueProperty, newBind)

通过创建Binding,您可以在WPF的数据网格中设置SelectedValue。