How to add Data to a WPF datagrid programatically

后端 未结 3 1496
没有蜡笔的小新
没有蜡笔的小新 2021-01-12 21:54

How can I add data Items to a DataGrid programmatically in WPF which do not have bindings? The DataGrid has 4 columns.

3条回答
  •  囚心锁ツ
    2021-01-12 22:40

    .xaml:

    
        
            
            
        
    
    

    .cs:

    public class DataItem
    {
        public bool Column1 { get; set; }
        public string Column2 { get; set; }
    }
    
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataItem item = new DataItem();
            item.Column1 = true;
            item.Column2 = "test";
            dataGrid.Items.Add(item);
        }
    }
    

提交回复
热议问题