首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过C#代码创建DataGridTemplateColumn

通过C#代码创建DataGridTemplateColumn
EN

Stack Overflow用户
提问于 2012-01-09 02:06:02
回答 2查看 16K关注 0票数 16

我有一个我已经创建的动态数据网格。我通过后台代码为它创建了每一列。我在一个列上遇到了麻烦,我希望在不编辑时显示在文本块中,但在编辑时显示为组合框。我有一个事务的ObservableCollection。每个事务都有一个称为"Account“的类型。这是我到目前为止所知道的:

代码语言:javascript
复制
    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中的项目显示正常。现在的问题是,当选择一个新项目时,TextBlock中的文本不会随着新项目的更新而更新。

代码语言:javascript
复制
    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“属性是这样声明的:

代码语言:javascript
复制
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();
    }

下面是我的Transaction类:

代码语言:javascript
复制
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; }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-09 02:22:15

您将ItemsSource绑定到所选值、字符串或字符数组,因此每个字符都被用作一个项,ItemsSource绑定大概应该指向可以从中选择值的其他集合。

票数 4
EN

Stack Overflow用户

发布于 2012-05-23 14:47:02

代码语言:javascript
复制
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)

通过创建绑定,您可以在数据网格中为WPF设置SelectedValue。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8779893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档