binding each item on gridview row
-
12-12-2019 - |
Pregunta
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>
Solución
This works.
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewRowPresenter}}, Path=DataContext}"
Otros consejos
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}"
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow