如何在AutoGenerateColumns="True"时将DataGrid的特定列设置为ComboBox

4

我是一个MVVM的新手。我在我的项目中使用WPF和MVVM。所以在开始编写应用程序之前,我正在测试一些东西。

我的页面(EmpDetailsWindow.xaml)像这样

<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)如下:

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。现在我有一个名为“Gender”的列在datagrid中。这应该是一个下拉框,下拉框的项目源来自视图模型的GenderDataTable。我该如何实现这个功能?

2个回答

7
你可以这样做:

你可以像这样做

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

Infragistics grid不支持该事件?如何处理这种情况? - Kuntady Nithesh
如果它真的不支持任何等效事件,我会联系Infragistics询问他们出了什么问题,然后禁用列的自动生成并明确定义它们。 - Dtex

0

这里似乎没有完整的答案,所以我将发布我从这个问题和实验中发现的内容。我确定这违反了许多规则,但它很简单并且有效。

public partial class MainWindow : Window
    {
        // define a dictionary (key vaue pair). This is your drop down code/value
        public static Dictionary<string, string> 
              dCopyType = new Dictionary<string, string>() { 
                 { "I", "Incr." }, 
                 { "F", "Full" } 
              };


// If you autogenerate columns, you can use this event 
// To selectively override each column
// You need to define this event on the grid in the event tab in order for it to be called
private void Entity_AutoGeneratingColumn(object sender,
                                         DataGridAutoGeneratingColumnEventArgs e)
        {

            // The name of the database column
            if (e.PropertyName == "CopyType")
            {
                // heavily based on code above
                var cb = new DataGridComboBoxColumn();
                cb.ItemsSource = dCopyType;  // The dictionary defined above
                cb.SelectedValuePath = "Key";  
                cb.DisplayMemberPath = "Value";
                cb.Header = "Copy Type"; 
                cb.SelectedValueBinding = new Binding("CopyType");
                e.Column = cb;
            }


        }
  } // end public partial class MainWindow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接