datagridtemplatecolumn 背后的代码是什么,以及如何使用它? [英] What is the code behind for datagridtemplatecolumn, and how to use it?

查看:25
本文介绍了datagridtemplatecolumn 背后的代码是什么,以及如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 中有一个 DataGrid.在绑定到特定的 ItemsSource 之后,我试图将 Button 添加到网格的某些单元格.我试图在 xaml 中这样做:

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. I have tried to do this in the xaml like this:

<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 .任何帮助将不胜感激.

However, I want to know as to how I can do this in the code behind. I need this so that I can place Buttons whenever a particular click even takes place. Any help will be highly appreciated.

推荐答案

使用这个:

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 中添加了 CheckBox.希望这有帮助!!

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

这篇关于datagridtemplatecolumn 背后的代码是什么,以及如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆