【问题标题】:how to make particular column of datagrid a combobox when AutoGenerateColumns="True"当 AutoGenerateColumns="True" 时如何使数据网格的特定列成为组合框
【发布时间】:2012-11-29 09:25:35
【问题描述】:

我是 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 已成功绑定到数据表。现在我在数据网格中有一个“性别”列。这应该是一个组合框,并且组合框的项目源来自视图模型的 GenderDataTable 。我怎样才能做到这一点?

【问题讨论】:

    标签: wpf mvvm


    【解决方案1】:

    你可以这样做

    <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 网格不支持此事件?为 dat 案做什么?
    • 好吧,如果它真的不支持任何等效事件,我会联系 Infragistics 询问它们有什么问题,然后继续禁用列的自动生成并明确定义它们
    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 2021-02-16
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多