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

发布于 2024-08-11 18:55:54 字数 620 浏览 4 评论 0原文

我在 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 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>

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧夏天 2024-08-18 18:55:54

使用这个:

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。
希望这有帮助!

use this:

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

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

梦在夏天 2024-08-18 18:55:54

如果您想在实例化网格之前添加按钮,特别是在将列添加到网格之前,Anurag 的答案将非常适合您。

如果您想要在网格构建完成后将按钮添加到网格单元格,您可以通过更改 DataGridCell 对象来实现。首先,您必须找到它:

  1. 使用 DataGridColumn.GetCellContent 查找 DataGridCell
  2. 使用 VisualTreeHelper 将可视化树向上扫描到 DataGridCell 将

完成此操作后,有多种方法可以将按钮添加到 DataGridCell,具体取决于您想要实现的目标:

  • DataGridCell.Template 设置为包含按钮和其他内容的 ControlTemplate您想要的样式,-或者-
  • DataGridCell.ContentTemplate 设置为包含您想要的按钮和其他项目的 DataTemplate,-或者-
  • 让列的 DataTemplate 包含一个用于保存的占位符面板新按钮,按名称在可视化树中搜索此面板,然后将按钮添加到其中。

不需要查找单元格的另一种方法是:

  1. 在视图模型中包含 ObservableCollection 属性,该属性提供
  2. DataTemplate 中 创建按钮的信息包含一个引用此属性的 ItemsControl ,并具有一个 DataTemplate ,可以创建类型为 T 的正确按钮
  3. 当您想要添加按钮时,只需将一个项目添加到 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:

  1. Find the DataGridCell by using DataGridColumn.GetCellContent
  2. Use VisualTreeHelper to scan up the visual tree to the DataGridCell

Once this is done, there are several ways to add a button to the DataGridCell, depending on what you're trying to achieve:

  • Set DataGridCell.Template to a ControlTemplate containing the buttons and other styling you desire, -OR-
  • Set DataGridCell.ContentTemplate to a DataTemplate containing the buttons and other items you desire, -OR-
  • Have your column's DataTemplate include a placeholder panel to hold new buttons, search down the visual tree for this panel by Name, and add your button to it.

An alternative approach that doesn't require finding the cell is to:

  1. Include an ObservableCollection<T> property in your view model that supplies the information to create the buttons
  2. In your DataTemplate include an ItemsControl that reference this property and has a DataTemplate that can create the correct button out of type T
  3. When you want to add a button, just add an item to the ObservableCollection property
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文