C# DataGrid列模板中的按钮交互

C# DataGrid列模板中的按钮交互,c#,wpf,datagrid,buttonclick,C#,Wpf,Datagrid,Buttonclick,在一个C#WPF程序中,我有一个网格,我已经成功地用我的数据填充了它。一列有一个按钮,我想链接到编辑页面。代码如下 var col = new DataGridTemplateColumn(); col.Header = "Edit"; var template = new DataTemplate(); var textBlockFactory = new FrameworkElementFactory(typeof(Button)); textBlockFactory.SetBinding(

在一个C#WPF程序中,我有一个网格,我已经成功地用我的数据填充了它。一列有一个按钮,我想链接到编辑页面。代码如下

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
}

我的理解是,您需要单击文本块的
rumId
,才能对其进行编辑

在设置
事件时,单击
可以执行以下操作

textBlockFactory.AddHandler( Button.ClickEvent, 
     new RoutedEventHandler((o, e) => 
      {
         var thisTextBlock = (TextBlock) o; // casting the sender as TextBlock 
         var rumID = int.Parse(thisTextBlock.Name); // since you bind the name of the text block to rumID
         Edit(rumID); // this method where you can do the editting 
      })); 

我的理解是,您需要单击文本块的
rumId
,才能对其进行编辑

在设置
事件时,单击
可以执行以下操作

textBlockFactory.AddHandler( Button.ClickEvent, 
     new RoutedEventHandler((o, e) => 
      {
         var thisTextBlock = (TextBlock) o; // casting the sender as TextBlock 
         var rumID = int.Parse(thisTextBlock.Name); // since you bind the name of the text block to rumID
         Edit(rumID); // this method where you can do the editting 
      })); 

只需将
Id
,或者更好地将整个数据对象绑定为
CommandParameter

void ButtonClick(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("hi Edit Click 1");

    Button b = sender as Button;

    // Get either the ID of the record, or the actual record to edit 
    // from b.CommandParameter and do something with it
}
如果您决定切换您的应用程序,使其使用MVVM设计模式,这也会起作用。例如,XAML如下所示:

<Button Command="{Binding EditCommand}"
        CommandParameter="{Binding }" />

只需将
Id
绑定,或者更好地将整个数据对象绑定为
CommandParameter

void ButtonClick(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("hi Edit Click 1");

    Button b = sender as Button;

    // Get either the ID of the record, or the actual record to edit 
    // from b.CommandParameter and do something with it
}
如果您决定切换您的应用程序,使其使用MVVM设计模式,这也会起作用。例如,XAML如下所示:

<Button Command="{Binding EditCommand}"
        CommandParameter="{Binding }" />


为了获得更大的灵活性,我宁愿使用其他控件而不是DataGrid。我已经用一个列表框、数据模板和一点代码来使用行和按钮做了一些事情。如果您希望使用其他控件而不是DataGrid来获得更大的灵活性,我可以为您提供一些示例。我已经用一个列表框、数据模板和一点代码来使用行和按钮做了一些事情。如果wishIt无法调用ButtonClick,我可以为您提供一些示例。@RogerStark只需更改设置
Click
事件的代码行,使其指向您的方法,而不是内联定义:
textBlockFactory.AddHandler(Button.ClickEvent,ButtonClick)谢谢,到目前为止效果不错,现在我需要提取id+发送者{System.Windows.Controls.Button:5}对象{System.Windows.Controls.Button}我想提取'5'@RogerStark将你的
发送者
转换成一个
按钮
,就像我在回答中一样,然后从
按钮内容
中的任何一个获取
5
按钮.DataContext
,或
按钮.CommandParameter
。我建议使用
Button.CommandParameter
,但这意味着您必须在首次创建按钮时设置绑定:
textBlockFactory.SetBinding(Button.CommandParameterProperty,new System.Windows.Data.binding(“rumId”)已解决,感谢您的帮助,我只编写了2周的c#程序,因此我是一个真正的新手。它还没有达到调用按钮单击的程度,这正是我遇到的问题。@RogerStark只需更改设置
Click
事件的代码行,使其指向您的方法,而不是内联定义:
textBlockFactory.AddHandler(Button.ClickEvent,ButtonClick);
谢谢,到目前为止,这一切都很有效,现在我需要提取id+发送者{System.Windows.Controls.Button:5}对象{System.Windows.Controls.Button}我想提取“5”@RogerStark将你的
发送者
转换成一个
按钮
,就像我在回答中所做的那样,然后从
按钮.Content
按钮.DataContext
按钮.CommandParameter
中获取
5
。我建议使用
按钮.CommandParameter
,但这意味着你没有o首次创建按钮时设置绑定:
textBlockFactory.SetBinding(button.CommandParameterProperty,new System.Windows.Data.binding(“rumId”);
已解决,感谢您的帮助,我编写c#才两周,所以我是一个真正的新手。