在C#WPF程序中,我有一个已成功填充数据的网格。一列有一个我想链接到编辑页面的按钮。代码如下。DataGrid列模板中的按钮交互
var col = new DataGridTemplateColumn();
col.Header = "Edit";
var template = new DataTemplate();
var textBlockFactory = new FrameworkElementFactory(typeof(Button));
textBlockFactory.SetBinding(Button.ContentProperty, new System.Windows.Data.Binding("rumId"));
textBlockFactory.SetBinding(Button.NameProperty, new System.Windows.Data.Binding("rumId"));
textBlockFactory.AddHandler(Button.ClickEvent, new RoutedEventHandler((o, e) => System.Windows.MessageBox.Show("TEST")));
template.VisualTree = textBlockFactory;
col.CellTemplate = template;
template = new System.Windows.DataTemplate();
var comboBoxFactory = new FrameworkElementFactory(typeof(Button));
template.VisualTree = comboBoxFactory;
col.CellEditingTemplate = template;
dgData.Columns.Add(col);
代码成功运行,每当我选择一个按钮时我都会收到一个消息框。
我怎样才能调用另一种方法,然后从中检索我选择的按钮的行号?
随后的方法看起来像,我怎么称呼它?
void ButtonClick(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("hi Edit Click 1");
// get the data from the row
string s = myRumList.getRumById(rumid).getNotes();
// do something with s
}
我宁愿用其他的控制比DataGrid中才能有更大的灵活性。我做了一些使用行列和按钮的ListBox,数据模板和一点代码隐藏功能。如果你愿意,我可以给你一些例子 – Gab 2012-02-17 18:55:46