简体   繁体   English

向 WPF Datagrid 添加多个按钮

[英]Add multiple buttons to WPF Datagrid

I want to add 2-3 buttons to the rows in the last column of my datagrid using the backend C# and not XAML.我想使用后端 C# 而不是 XAML 向数据网格最后一列中的行添加 2-3 个按钮。 I managed to add one button to the cells but I'm having trouble adding anymore past that.我设法在单元格中添加了一个按钮,但我无法再添加。

I have tried creating a new FrameworkElementFactory and adding it into the column but it just replaces the previous button instead of adding the button.我尝试创建一个新的 FrameworkElementFactory 并将其添加到列中,但它只是替换了上一个按钮而不是添加按钮。

        DataGridTemplateColumn buttonColumn = new DataGridTemplateColumn();
        buttonColumn.Header = "Actions";
        buttonColumn.Width = 209;

        DataTemplate buttonTemplate = new DataTemplate();
        FrameworkElementFactory buttonFactory = new FrameworkElementFactory(typeof(Button));
        buttonTemplate.VisualTree = buttonFactory;

        buttonFactory.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(Activate));
        buttonFactory.SetValue(ContentProperty, "A");
        buttonColumn.CellTemplate = buttonTemplate;

        dGrid_SavedData.Columns.Add(buttonColumn);

Regardless of whether you create it programmatically or in XAML, a DataTemplate can only have single root element so you should set the VisualTree property to a FrameworkElementFactory for a Panel and use the AppendChild method to add the button factories to the panel factory, eg:无论您是以编程方式还是在 XAML 中创建它, DataTemplate只能具有单个根元素,因此您应该将VisualTree属性设置为PanelFrameworkElementFactory并使用AppendChild方法将按钮工厂添加到面板工厂,例如:

DataGridTemplateColumn buttonColumn = new DataGridTemplateColumn();
buttonColumn.Header = "Actions";
buttonColumn.Width = 209;

DataTemplate buttonTemplate = new DataTemplate();
FrameworkElementFactory panelFactory = new FrameworkElementFactory(typeof(StackPanel));
buttonTemplate.VisualTree = panelFactory;

FrameworkElementFactory buttonAFactory = new FrameworkElementFactory(typeof(Button));
buttonAFactory.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(Activate));
buttonAFactory.SetValue(ContentProperty, "A");

FrameworkElementFactory buttonBFactory = new FrameworkElementFactory(typeof(Button));
buttonBFactory.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(Activate));
buttonBFactory.SetValue(ContentProperty, "B");

panelFactory.AppendChild(buttonAFactory);
panelFactory.AppendChild(buttonBFactory);

buttonColumn.CellTemplate = buttonTemplate;

dGrid_SavedData.Columns.Add(buttonColumn);

So, I'm on an iPad, so I can't give much detail.所以,我在 iPad 上,所以我不能提供太多细节。 First let me say that this is a very hard way to do this and I recommend just about anything else.首先让我说这是一种非常困难的方法,我推荐其他任何方法。 But if you absolutely have to fully dynamically generate your grid, you need to put the button in a container.但是,如果您绝对必须完全动态地生成网格,则需要将按钮放入容器中。 For example, a vertical stack panel.例如,垂直堆栈面板。 Then you can add as many buttons as you want.然后,您可以根据需要添加任意数量的按钮。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM