通过C#代码创建DataGridTemplateColumn

我有一个我创建的动态Datagrid。 我通过后面的代码为它创建每一列。 我在列上遇到麻烦,我希望在不编辑时显示在文本块中,而是在编辑时作为combobox显示。 我有一个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中。 但是,在combobox中,每个项目只显示一个字符。 例如,这是文本块:

在此处输入图像描述

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

在此处输入图像描述

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

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

 public ObservableCollection Accounts { get; set; } public MainWindow() { this.Types = new ObservableCollection(); this.Parents = new ObservableCollection(); this.Transactions = new ObservableCollection(); this.Accounts = new ObservableCollection(); 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; } } } 

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

 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。