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

标签 wpf datagrid datagridtemplatecolumn

我在 WPF 中有一个 DataGrid。在绑定(bind)到特定的 ItemsSource 后,我尝试将 Button 添加到网格的某些单元格。我尝试在 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>

但是,我想知道如何在后面的代码中做到这一点。我需要这个,以便我可以在发生特定点击时放置 Button 。任何帮助将不胜感激。

最佳答案

使用这个:

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);

我使用它在运行时在我的 DataGridTemplateColumn 中添加复选框。 希望这有帮助!!

关于wpf - datagridtemplatecolumn 背后的代码是什么,以及如何使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1754608/

相关文章:

c# - 为什么这段代码似乎有错误?

c# - 用于更新数据集的 OracleDataAdapter CommandText

wpf - 如何对 WPF 工具包 DataGrid 上的 DataGridTemplateColumn 进行排序?

wpf - 可编辑的 DataGrid - CanUserAddRows ="True"不起作用

wpf - 无法在分层数据模板 WPF 内的组合框上使用附加属性

c# - 创建一个覆盖层以禁用所有其他表单元素

c# - DataGrid 行标题 WPF

c# - 根据值更改 DataGrid 单元格颜色

c# - WPF DataGrid 中基于基础数据集(及其类型)的动态 DataGrid 列

wpf - 如何使用 Prism 6 正确制作 CanExecute 触发器