Question

Suppose I have a XAML markup as below. How do I bind the command parameter of the button to each row. Please show the most appropriate binding expression.

<ListView ItemsSource="{Binding Items}">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Items" Width="Auto">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <TextBlock Text="{Binding Path=Name, Mode=OneWay}"/>
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
    <GridViewColumn Header="" Width="Auto">
      <GridViewColumn.CellTemplate>
        <DataTemplate>
          <Button MinWidth="100" Command="{Binding DeleteCommand}" CommandParameter="{Binding <EXPRESSION>}" >Delete</Button>
        </DataTemplate>
      </GridViewColumn.CellTemplate>
    </GridViewColumn>
  <ListView.View>
</ListView>
Was it helpful?

Solution

This works.

CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewRowPresenter}}, Path=DataContext}"

OTHER TIPS

I hate WPF.

Create a self-referential property

Add this property to your bound datamodel class:

public class MyDataObjectItem
{
    //...
    public MyDataObjectItem Self { get { return this; } }
    //...
}

Then the command parameter is easy:

CommandParameter="{Binding Self}"

Or, apparently this works

CommandParameter="{Binding}"

see https://stackoverflow.com/a/11287478/887092

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top