datagridtemplatecolumn 背后的代码是什么,以及如何使用它?
我在 WPF 中有一个 DataGrid。在绑定到特定的 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
。任何帮助将不胜感激。
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>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用这个:
我使用它在运行时在我的 DataGridTemplateColumn 中添加 CheckBox。
希望这有帮助!
use this:
I used this to add CheckBox in my DataGridTemplateColumn at runtime.
Hope this helps!!
如果您想在实例化网格之前添加按钮,特别是在将列添加到网格之前,Anurag 的答案将非常适合您。
如果您想要在网格构建完成后将按钮添加到网格单元格,您可以通过更改 DataGridCell 对象来实现。首先,您必须找到它:
DataGridColumn.GetCellContent
查找DataGridCell
VisualTreeHelper
将可视化树向上扫描到DataGridCell 将
完成此操作后,有多种方法可以将按钮添加到 DataGridCell,具体取决于您想要实现的目标:
DataGridCell.Template
设置为包含按钮和其他内容的 ControlTemplate您想要的样式,-或者-DataGridCell.ContentTemplate
设置为包含您想要的按钮和其他项目的 DataTemplate,-或者-DataTemplate
包含一个用于保存的占位符面板新按钮,按名称
在可视化树中搜索此面板,然后将按钮添加到其中。不需要查找单元格的另一种方法是:
ObservableCollection
属性,该属性提供DataTemplate
中 创建按钮的信息包含一个引用此属性的ItemsControl
,并具有一个DataTemplate
,可以创建类型为T
的正确按钮ObservableCollection
属性中Anurag's answer will work very well for you if you want to add the buttons before the grid is instantiated, specifically before you add the column to the grid.
If you want to add the button to the grid cell after the grid is already built, you can do it by making changes to the DataGridCell object. First you have to find it:
DataGridCell
by usingDataGridColumn.GetCellContent
VisualTreeHelper
to scan up the visual tree to theDataGridCell
Once this is done, there are several ways to add a button to the DataGridCell, depending on what you're trying to achieve:
DataGridCell.Template
to a ControlTemplate containing the buttons and other styling you desire, -OR-DataGridCell.ContentTemplate
to a DataTemplate containing the buttons and other items you desire, -OR-DataTemplate
include a placeholder panel to hold new buttons, search down the visual tree for this panel byName
, and add your button to it.An alternative approach that doesn't require finding the cell is to:
ObservableCollection<T>
property in your view model that supplies the information to create the buttonsDataTemplate
include anItemsControl
that reference this property and has aDataTemplate
that can create the correct button out of typeT
ObservableCollection
property