WPF DataGrid DataGridTemplateColumn ComboBox ItemSource

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17430988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:07:57  来源:igfitidea点击:

WPF DataGrid DataGridTemplateColumn ComboBox ItemSource

wpfdatagridcomboboxdatagridtemplatecolumnitemsource

提问by Matt Ellenburg

I'm trying to create a very simple CRUD WPF application. I have a datagrid with 2 columns, ID, and Category. For category, I want to be able to select from a list when adding and editing. Here's what I have so far in the xaml:

我正在尝试创建一个非常简单的 CRUD WPF 应用程序。我有一个包含 2 列、ID 和类别的数据网格。对于类别,我希望能够在添加和编辑时从列表中进行选择。到目前为止,这是我在 xaml 中的内容:

<DataGrid Name="dataGridBudgetEntries" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" RowEditEnding="dataGridBudgetEntries_RowEditEnding">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding ID}" Header="ID" IsReadOnly="True"></DataGridTextColumn>
        <DataGridTemplateColumn Header="Category">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding CategoryName}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox></ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

In the code behind, I have the following:

在后面的代码中,我有以下内容:

public MainWindow()
{
    InitializeComponent();

    dataGridBudgetEntries.ItemsSource = FinancialManagementDatabase4ME.BLL.GetBudgetEntriesForDataGrid();
    List<Category> categories = FinancialManagementDatabase4ME.BLL.GetCategories();
}

The grid is populating and the display value for category is correct. I cannot figure out how to define the combobox in the datatemplate so that it displays the selected value along with a list of other categories. I've seen tons of examples but cannot get any to work.

网格正在填充并且类别的显示值是正确的。我无法弄清楚如何在数据模板中定义组合框,以便它显示选定的值以及其他类别的列表。我已经看到了大量的例子,但无法得到任何工作。

回答by Nathan

 <DataGrid AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" ItemsSource="{Binding Entries}" RowEditEnding="dataGridBudgetEntries_RowEditEnding">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding ID}" Header="ID" IsReadOnly="True"></DataGridTextColumn>
        <DataGridTemplateColumn Header="Category">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Category}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding Categories}"></ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>


public MainWindow()
{
    InitializeComponent();
    this.DataContext = new MyViewModel();
}

public class MyViewModel : ViewModelBase
{
    public MyViewModel()
    {
        this.Entries = FinancialManagementDatabase4ME.BLL.GetBudgetEntriesForDataGrid();
        this.Categories = FinancialManagementDatabase4ME.BLL.GetCategories();
    }

    private ObservableCollection<BudgetEntries> entries;
    public ObservableCollection<BudgetEntries> Entries
    {
        get
        {
            return this.entries;
        }

        set
        {
            if (value != this.entries)
            {
                this.entries = value;
                this.OnPropertyChanged("Entries");
            }
        }
    }

    private ObservableCollection<Category> categories;
    public ObservableCollection<Category> Categories
    {
        get
        {
            return this.categories;
        }

        set
        {
            if (value != this.categories)
            {
                this.categories = value;
                this.OnPropertyChanged("Categories");
            }
        }
    }
}

public class BudgetEntries : ViewModelBase
{
    private string id;
    public string Id
    {
        get
        {
            return this.id;
        }

        set
        {
            if (value != this.id)
            {
                this.id = value;
                this.OnPropertyChanged("Id");
            }
        }
    }

    private string category;
    public string Category
    {
        get
        {
            return this.category;
        }

        set
        {
            if (value != this.category)
            {
                this.category = value;
                this.OnPropertyChanged("Category");
            }
        }
    }
}

回答by dylansweb

You need to inherit the INotifyPropertyChanged interface in your data item class (the type you are using to give each row data), and use the PropertyChangedEventHandler to be able to notify the datagrid which item should be selected according to the property of your data type, as well as update the property on that type, for that row, whenever a selection is made in the combobox. Take a look at this blog entry titled: WPF Datagrid Combobox Column at: http://www.dylansweb.com. It will be just what you need I believe.

你需要在你的数据项类(你用来给每一行数据的类型)中继承 INotifyPropertyChanged 接口,并使用 PropertyChangedEventHandler 来通知数据网格应该根据你的数据类型的属性选择哪个项目,以及更新该类型的属性,对于该行,每当在组合框中进行选择时。看一看这篇名为:WPF Datagrid Combobox Column 的博客条目:http://www.dylansweb.com 。我相信这正是您所需要的。