问题描述
我有我创建了一个动态的Datagrid。我通过代码创建它的每一列后面。我有一列的麻烦,我想在不编辑时,文本块中显示,但在编辑的组合框。我有交易一个ObservableCollection。每笔交易都有一个名为帐户类型。以下是我迄今为止:
私人DataGridTemplateColumn GetAccountColumn()
{
//创建列
DataGridTemplateColumn accountColumn =新DataGridTemplateColumn();
accountColumn.Header =账户;
结合绑定=新的绑定(账户);
bind.Mode = BindingMode.TwoWay;
//创建文本块
FrameworkElementFactory textFactory =新FrameworkElementFactory(typeof运算(TextBlock中));
textFactory.SetBinding(TextBlock.TextProperty,绑定);
的DataTemplate textTemplate =新的DataTemplate();
textTemplate.VisualTree = textFactory;
//创建组合框
bind.Mode = BindingMode.OneWay;
FrameworkElementFactory comboFactory =新FrameworkElementFactory(typeof运算(组合框));
comboFactory.SetValue(ComboBox.DataContextProperty,this.Transactions);
comboFactory.SetValue(ComboBox.IsTextSearchEnabledProperty,真);
comboFactory.SetBinding(ComboBox.ItemsSourceProperty,绑定);
的DataTemplate comboTemplate =新的DataTemplate();
comboTemplate.VisualTree = comboFactory;
//设置模板到列
accountColumn.CellTemplate = textTemplate;
accountColumn.CellEditingTemplate = comboTemplate;
返回accountColumn;
}
该值显示在文本块。然而,在组合框,我只得到一个字符每个项目可以显示。例如,下面是正文块:
但是,当我点击编辑,进入组合框,这里是所示:
有人可以帮助我出这么在下拉列表中的项目正常显示? 。此外,当我从下拉框中选择东西时,文本块不会更新与我选择的项目
更新:
下面是我的专栏,截至目前。在ComboBox中的项目正在正常显示。现在的问题是,当选择一个新的项目,在TextBlock的文本不会更新与新项目。
私人DataGridTemplateColumn GetAccountColumn()
{
//创建列
DataGridTemplateColumn accountColumn =新DataGridTemplateColumn();
accountColumn.Header =账户;
结合绑定=新的绑定(账户);
bind.Mode = BindingMode.OneWay;
//创建文本块
FrameworkElementFactory textFactory =新FrameworkElementFactory(typeof运算(TextBlock中));
textFactory.SetBinding(TextBlock.TextProperty,绑定);
的DataTemplate textTemplate =新的DataTemplate();
textTemplate.VisualTree = textFactory;
//创建组合框
结合comboBind =新的绑定(账户);
comboBind.Mode = BindingMode.OneWay;
FrameworkElementFactory comboFactory =新FrameworkElementFactory(typeof运算(组合框));
comboFactory.SetValue(ComboBox.IsTextSearchEnabledProperty,真);
comboFactory.SetValue(ComboBox.ItemsSourceProperty,this.Accounts);
comboFactory.SetBinding(ComboBox.SelectedItemProperty,comboBind);
的DataTemplate comboTemplate =新的DataTemplate();
comboTemplate.VisualTree = comboFactory;
//设置模板到列
accountColumn.CellTemplate = textTemplate;
accountColumn.CellEditingTemplate = comboTemplate;
返回accountColumn;
}
帐户属性声明这样在我的MainWindow类:
公开的ObservableCollection<串GT;帐户{搞定;组; }
公共主窗口()
{
this.Types =新的ObservableCollection<串GT;();
this.Parents =新的ObservableCollection<串GT;();
this.Transactions =新的ObservableCollection<&交易GT;();
this.Accounts =新的ObservableCollection<串GT;();
的openDatabase();
的InitializeComponent();
}
下面是我的事务类:
公共类交易
{
私人字符串日期;
私人串号;
私人串账户;
公共字符串日期
{
{返回日期; }
集合{日期=值; }
}
公共串号
{
{返回数; }
集合{数=价值; }
}
公共字符串帐户
{
{返回账户; }
集合{账户=价值; }
}
}
您绑定的ItemsSource
来选定的值,一个字符串,又名字符数组,所以每一个字符作为一个项目,在的ItemsSource
结合想必应该针对可从中选择的值一些其他的集合。
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;
}
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:
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:
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;
}
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();
}
Here is my Transaction Class:
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; }
}
}
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!