通过C#代码创建DataGridTemplateColumn [英] Create DataGridTemplateColumn Through C# Code
问题描述
我有一个我创建的动态Datagrid。我通过代码创建每个列。我在一个列上遇到麻烦,我想要在不编辑时显示在文本块,而在编辑时作为组合框。我有一个ObservableCollection的事务。每个事务具有称为帐户的类型。这是我到目前为止:
I have a dynamic Datagrid that I have created. I am creating each column for it through code behind. I am having troubles on a column that I want to be displayed at a textblock when not editing, but as a combobox while editing. I have an ObservableCollection of Transactions. Each Transaction has a type called "Account". Here is what I have so far:
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中。但是,在组合框中,我每个项目只显示一个字符。例如,以下是文字块:
The value displays in the TextBlock. However, in the combobox, I am only getting one character to display per item. For example, here is the textblock:
但是当我点击编辑并进入组合框时,显示如下:
But when I click to edit and go into the combobox, here is what is shown:
有人可以帮我以便Combobox中的项目正确显示?此外,当我从Combobox中选择一个内容时,文本块不会用我选择的项更新。
Can someone help me out so that the items in the Combobox are displayed properly? Also, when I select something from the Combobox, the textblock isn't updated with the item I selected.
UPDATED:
这是我现在的专栏。 ComboBox中的项目正在正确显示。现在的问题是,当选择一个新项目时,TextBlock中的文本不会被新项目更新。
Here is my column as of now. The items in the ComboBox are being displayed properly. The issue now is that when a new item is selected, the text in the TextBlock isn't updated with the new item.
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类中声明如下: p>
The "Accounts" property is declared like this in my MainWindow class:
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; }
}
}
推荐答案
您将 ItemsSource
绑定到所选的值,一个字符串,一个字符数组,所以每个字符都用作一个项目, ItemsSource
binding可能应该定位到可以从中选择值的其他集合。
You bind the ItemsSource
to the selected value, a string, aka char array, so every character is used as an item, the ItemsSource
binding presumably should target some other collection from which the value can be chosen.
这篇关于通过C#代码创建DataGridTemplateColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!