Can Datagrid column contain diffterent type of control in different row

前端 未结 2 2083
闹比i
闹比i 2021-02-09 11:16

In Wpf, Can I create a datagrid which different row contain different type of control in same column?

For easiest case: datagrid with 5 cols, 2 rows, don\'t care about 4

2条回答
  •  滥情空心
    2021-02-09 11:52

    You can use a DataGridTemplateColumn combined with a few triggers to achieve this functionality.

    This is a demo application that binds a DataGrid to a list of (string) Control Types. The first column just displays the control type string, and the second column acts on the same information to present the corresponding Control. You might be able to make the xaml a bit more concise, but this is the jist of it:

    The XAML:

    
    
        
            
            
                
                    
                        
                            
                                
                                    
                                
                            
                        
                    
                
            
        
    
    

    Code-behind and View Model:

    namespace DataGridWithMultipleTypesPerColumn
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new ViewModel();
            }
        }
    
        public class ViewModel
        {
            public ObservableCollection ControlTypes
            {
                get;
                private set;
            }
            public ViewModel()
            {
                ControlTypes = new ObservableCollection() { "Button", "TextBox", "CheckBox" };
            }
        }
    }
    

提交回复
热议问题