I have a ListView in my MVVM WPF implementation, it has a DataTemplate with a button inside. The ListView is bound to a collection of complex objects in the ViewModel.
<ListView ItemsSource="{Binding Path=ComplexObjects}"
SelectedItem="{Binding Path=SelectedObject}"
Width="Auto">
<ListView.View>
<GridView>
<GridViewColumn Header="My Property">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<TextBlock Text="{Binding MyProperty}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="First Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<Button Command="{Binding ???}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
All the text fields are bound with no problem, but can I bind the Button Command to a member method of the ComplexObject? If so is there any way to pass parameters?
I have a feeling I'm probably just evading using an ICommand.
Thanks.
Yes for Button you will need to use ICommand established in your ComplexObject:
If your method is in the View Model that holds the ComplexObjects collection name your control or window Me and use something like this:
You could use click or preview mouse down, but if you're wanting to bind the method in XAML, you would still need to route that event to a command. It's just easier to bind the command.
You could bind an
ICommand
on the ViewModel, passing the instance to invoke the method on as a parameter. The ViewModel can then call the appropriate method on the rightComplexObject
.For example:
Then the viewmodel could look like so:
If you really wanted to get crazy I suppose you could make your own button custom control with a dependency property that is a
delegate
, and you could then bind to a property that returns the same type ofdelegate
. And then in your custom control class you could invoke the delegate when it gets clicked.There is always the standard ICommand, if you go that route this is how...
Use Kent Boogaart's DelegateCommand class from his blog post. Then...
In the class you are binding to: