datagridtemplatecolumn背后的代码是什么,以及如何使用它?

Gag*_*gan 11 wpf datagrid datagridtemplatecolumn

我有一个DataGridWPF.而且我试图将Buttons 添加到网格的某些单元格中,然后绑定到特定的单元格ItemsSource.我试图在xaml中这样做:

<dg:DataGridTemplateColumn x:Name="R1" CanUserReorder="False" IsReadOnly="False">             
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <awc:ImageButton Content="Edit" Name="btnEdit" Visibility="Collapsed"/>
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)

但是,我想知道如何在后面的代码中执行此操作.我需要这个,以便Button每当特定点击发生时我都可以放置.任何帮助将受到高度赞赏.

vik*_*iky 26

用这个:

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);
Run Code Online (Sandbox Code Playgroud)

我在运行时使用它在我的DataGridTemplateColumn中添加CheckBox.希望这可以帮助!!