What is the code behind for datagridtemplatecolumn, and how to use it?

前端 未结 2 1105
有刺的猬
有刺的猬 2020-12-01 10:05

I have a DataGrid in WPF. And I am trying to add Buttons to certain cells of the grid, after it is bound to a particular ItemsSource.

2条回答
  •  鱼传尺愫
    2020-12-01 10:44

    use this:

    DataGridTemplateColumn col1 = new DataGridTemplateColumn();
    col1.Header = "MyHeader";
    FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(CheckBox));
    Binding b1 = new Binding("IsSelected");
    b1.Mode = BindingMode.TwoWay;
    factory1.SetValue(CheckBox.IsCheckedProperty, b1);
    factory1.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(chkSelect_Checked));
    DataTemplate cellTemplate1 = new DataTemplate();
    cellTemplate1.VisualTree = factory1;
    col1.CellTemplate = cellTemplate1;
    dgTransportReqsts.DataGrid.Columns.Add(col1);
    

    I used this to add CheckBox in my DataGridTemplateColumn at runtime. Hope this helps!!

提交回复
热议问题