gpt4 book ai didi

c# - 通过C#代码创建DataGridTemplateColumn

转载 作者:可可西里 更新时间:2023-11-01 07:47:49 25 4
gpt4 key购买 nike

我有一个我创建的动态数据网格。我正在通过代码隐藏为其创建每一列。我在不编辑时想在文本 block 中显示但在编辑时作为组合框显示的列有问题。我有一个 ObservableCollection 交易。每个交易都有一个称为“帐户”的类型。这是我目前所拥有的:

    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 中。但是,在组合框中,我只能让每个项目显示一个字符。例如,这里是文本 block :

enter image description here

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

enter image description here

有人可以帮助我正确显示组合框中的项目吗?此外,当我从组合框中选择某些内容时,文本 block 不会更新为我选择的项目。

更新:

这是我目前的专栏。 ComboBox 中的项目正在正确显示。 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;
}

“帐户”属性在我的 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();
}

这是我的事务类:

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 绑定(bind)到所选值,一个字符串,又名 char 数组,因此每个字符都用作一个项目,ItemsSource 绑定(bind)大概应该针对其他一些可以从中选择值的集合。

关于c# - 通过C#代码创建DataGridTemplateColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8779893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com