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

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


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.

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


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

<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;}


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

Button in 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)        {        }    }