Search code examples
c#wpfbindingcommandparameter

binding each item on gridview row


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>

Solution

  • This works.

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