WPF DataGrid DataGridTemplateColumn ComboBox ItemSsource

时间:2013-07-02 16:34:42

标签: wpf datagrid combobox datagridtemplatecolumn itemsource

我正在尝试创建一个非常简单的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>

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

public MainWindow()
{
    InitializeComponent();

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

网格正在填充,类别的显示值正确。我无法弄清楚如何在datatemplate中定义组合框,以便它显示所选值以及其他类别的列表。我已经看过很多例子但却无法工作。

2 个答案:

答案 0 :(得分:3)

 <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");
            }
        }
    }
}

答案 1 :(得分:0)

您需要继承数据项类中的INotifyPropertyChanged接口(用于给每行数据的类型),并使用PropertyChangedEventHandler能够根据您的属性通知datagrid应选择哪个项目。每当在组合框中进行选择时,该行的数据类型以及更新该类型的属性。请查看标题为:WPF Datagrid Combobox Column的博文:http://www.dylansweb.com。这将是你需要我相信的。