使用DataTable中的值将按钮添加到DataGrid [英] Add Buttons to DataGrid with value from DataTable

查看:51
本文介绍了使用DataTable中的值将按钮添加到DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataTable,其中第一列是一组数字,并且此DataTable分配给了DataGrid:

I have a DataTable where the first column is a set of numbers, and this DataTable is assigned to a DataGrid:

dgDataGrid.ItemsSource = dtDataTable.AsDataView();


DataTable中的第一列是一组我希望在DataGrid中作为按钮使用的数字,然后该列每一行中的数字将成为每个按钮的值.


The first column that is in the DataTable is a set of numbers that I want to have as buttons in the DataGrid, and the number in each row of this column would then be the value of each button. 

我该怎么做?据我了解,DataTable本身没有按钮,所以我猜想添加按钮必须在绑定到DataGrid之后发生.

How do I do that? As I understand the DataTable itself can't have a button, so I guess that adding the button must occur after the binding to DataGrid.

到目前为止,我唯一要做的就是添加一个带有按钮的新列:

The only thing I have achieved so far is adding a new column with a button:

DataGridTemplateColumn buttonColumn = new DataGridTemplateColumn();
                                DataTemplate buttonTemplate = new DataTemplate();
                                FrameworkElementFactory buttonFactory = new FrameworkElementFactory(typeof(Button));
                                buttonTemplate.VisualTree = buttonFactory;
                                //add handler or you can add binding to command if you want to handle click
                                buttonFactory.AddHandler(Button.ClickEvent, new RoutedEventHandler(button1_Click));
                                buttonFactory.SetValue(ContentProperty, "Button");
                                buttonColumn.CellTemplate = buttonTemplate;
                                dgDataGrid.Columns.Add(buttonColumn);

但这不是我想要的:


我需要#"列中的数字是实际的按钮.

I need the "#" column with the numbers to be the actual buttons.

我该如何实现?

推荐答案

如果使用自动生成的列,则可以处理DataGrid的AutoGeneratingColumn事件:

If you are using auto-generated columns you could handle the AutoGeneratingColumn event for the DataGrid:

dgMatrix.AutoGeneratingColumn += (s, e) => 
            {
                if (e.PropertyName == "#")
                {
                    DataGridTemplateColumn buttonColumn = new DataGridTemplateColumn();
                    DataTemplate buttonTemplate = new DataTemplate();
                    FrameworkElementFactory buttonFactory = new FrameworkElementFactory(typeof(Button));
                    buttonTemplate.VisualTree = buttonFactory;
                    //add handler or you can add binding to command if you want to handle click
                    buttonFactory.AddHandler(Button.ClickEvent, new RoutedEventHandler(button1_Click));
                    buttonFactory.SetBinding(Button.ContentProperty, new Binding("#"));
                    buttonColumn.CellTemplate = buttonTemplate;
                    e.Column = buttonColumn;
                }
            };

如果没有,则可以像当前一样将列添加到DataGrid中.只需将Button的Content属性绑定到DataTable的相应列即可:

If not you could just add the column to the DataGrid like you are currently doing. Just bind the Content property of the Button to the appropriate column of the DataTable:

buttonFactory.SetBinding(Button.ContentProperty, new Binding("#")); // "#" is the name of the column in the DataTable

希望有帮助.

请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


这篇关于使用DataTable中的值将按钮添加到DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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