C# 列中的按钮,在 Click 事件处理程序中获取它来自的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1168976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:54:16  来源:igfitidea点击:

Button in a column, getting the row from which it came on the Click event handler

c#wpfxamldatagriddatagridview

提问by RekrowYnapmoc

I've set the itemsource of my WPF Datagrid to a List of Objects returned from my DAL. I've also added an extra column which contains a button, the xaml is below.

我已将 WPF Datagrid 的 itemsource 设置为从我的 DAL 返回的对象列表。我还添加了一个包含按钮的额外列,xaml 在下面。

<toolkit:DataGridTemplateColumn  MinWidth="100" Header="View">
    <toolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Click="Button_Click">View Details</Button>
        </DataTemplate>
    </toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

This renders fine. However on the Button_Clickmethod, is there any way I can get the row on the datagrid where the button resides? More specifically, one of the properties of my objects is "Id", and I'd like to be able to pass this into the constructor of another form in the event handler.

这很好。但是,在Button_Click方法上,有什么方法可以获取按钮所在的数据网格上的行?更具体地说,我的对象的一个​​属性是“Id”,我希望能够将它传递给事件处理程序中另一个表单的构造函数。

private void Button_Click(object sender, RoutedEventArgs e)
    {
        //I need to know which row this button is on so I can retrieve the "id"  
    }

Perhaps I need something extra in my xaml, or maybe I'm going about this in a roundabout way? Any help/advice appreciated.

也许我的 xaml 需要一些额外的东西,或者我可能会以迂回的方式解决这个问题?任何帮助/建议表示赞赏。

采纳答案by Jobi Joy

Basically your button will inherit the datacontext of a row data object. I am calling it as MyObject and hope MyObject.ID is what you wanted.

基本上,您的按钮将继承行数据对象的数据上下文。我称它为 MyObject 并希望 MyObject.ID 是您想要的。

private void Button_Click(object sender, RoutedEventArgs e)
{
    MyObject obj = ((FrameworkElement)sender).DataContext as MyObject;
    //Do whatever you wanted to do with MyObject.ID
}

回答by xr280xr

Another way I like to do this is to bind the ID to the CommandParameter property of the button:

我喜欢这样做的另一种方法是将 ID 绑定到按钮的 CommandParameter 属性:

<Button Click="Button_Click" CommandParameter="{Binding Path=ID}">View Details</Button>

Then you can access it like so in code:

然后你可以像这样在代码中访问它:

private void Button_Click(object sender, RoutedEventArgs e)
{
    object ID = ((Button)sender).CommandParameter;
}

回答by Shiha

MyObject obj= (MyObject)((Button)e.Source).DataContext;

回答by Petr ?ebesta

Another way which binds to command parameter DataContext and respect MVVM like Jobi Joy says button inherits datacontext form row.

另一种绑定到命令参数 DataContext 并像 Jobi Joy 这样尊重 MVVM 的方法说按钮继承 datacontext 表单行。

Button in XAML

XAML 中的按钮

<RadButton Content="..." Command="{Binding RowActionCommand}" 
                         CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=DataContext}"/>

Command implementation

命令实现

public void Execute(object parameter)
    {
        if (parameter is MyObject)
        {

        }
    }

回答by SurfingSanta

If your DataGrid's DataContext is a DataView object (the DefaultView property of a DataTable), then you can also do this:

如果您的 DataGrid 的 DataContext 是一个 DataView 对象(DataTable 的 DefaultView 属性),那么您也可以这样做:

private void Button_Click(object sender, RoutedEventArgs e) {
   DataRowView row = (DataRowView)((Button)e.Source).DataContext;
}