datagridtemplatecolumn背后的代码是什么,以及如何使用它? [英] What is the code behind for datagridtemplatecolumn, and how to use it?
本文介绍了datagridtemplatecolumn背后的代码是什么,以及如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在WPF中有一个 DataGrid
。而且我试图在网格的某些单元格绑定到特定的 ItemsSource
之后添加 Button
。我试图在xaml这样做:
I have a DataGrid
in WPF. And I am trying to add Button
s 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 Button
s 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屋!
查看全文