当AutoGenerateColumns ="True"时如何使数据网格的特定列成为组合框? [英] how to make particular column of datagrid a combobox when AutoGenerateColumns="True"

查看:82
本文介绍了当AutoGenerateColumns ="True"时如何使数据网格的特定列成为组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVVM的新手.我在项目中将wpf与MVVM一起使用.因此,在进入需要编写的应用程序之前,我现在正在测试事物.

I am new to MVVM . I am using wpf with MVVM in my project . So I am testing things right now before diving into an app I need to write.

我的页面(EmpDetailsWindow.xaml)就是这样

My page (EmpDetailsWindow.xaml) is like this

<Grid>
    <DataGrid Name="dgEmployee" Grid.Row="0" AutoGenerateColumns="True" ItemsSource="{Binding EmployeeDataTable}" CanUserAddRows="True" CanUserDeleteRows="True" IsReadOnly="False"  />
    <Button x:Name="btnSubmit" Content="Submit" Command="{Binding SubmitCommand}" CommandParameter="sample param" HorizontalAlignment="Left" Margin="212,215,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>

我的模型(EmpDetailsWindowViewModel)如下

and my model (EmpDetailsWindowViewModel) is as below

public class EmpDetailsWindowViewModel : INotifyPropertyChanged
    {
        public ICommand SubmitCommand { get; set; }
        public EmpDetailsWindowViewModel()
        {
            EmployeeDataTable = DataTableCreator.EmployeeDataTable();
            GenderDataTable = DataTableCreator.GenderDataTable();
            SubmitCommand = new SubmitCommand();
        }

        DataTable _employeeDataTable;
        public DataTable EmployeeDataTable
        {
            get { return _employeeDataTable;}
            set
            {
                _employeeDataTable = value;
                RaisePropertyChanged("EmployeeDataTable");
            }
        }

        DataTable _genderDataTable;
        public DataTable GenderDataTable
        {
            get { return _genderDataTable; }
            set
            {
                _genderDataTable = value;
                RaisePropertyChanged("GenderDataTable");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;


        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

datagrid已成功绑定到datatable.现在,我在datagrid中有一列性别".这应该是一个组合框,而cobobox的项源是从视图模型的GenderDataTable获取的.我该如何实现?

datagrid is successfully bound to the datatable . Now I have a column "Gender" in datagrid. This should be a combobox and the item source of the cobobox is got from GenderDataTable of the view model . How can I achieve this ?

推荐答案

您可以这样做

<DataGrid AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"/>

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyName == "Gender")
    {
        var cb = new DataGridComboBoxColumn();
        cb.ItemsSource = (DataContext as MyVM).GenderDataTable;
        cb.SelectedValueBinding = new Binding("Gender");
        e.Column = cb;
    }        
}

这篇关于当AutoGenerateColumns ="True"时如何使数据网格的特定列成为组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆