I want to capture button clicks from within a list item row. Below I have tried to boil the problem down as much as I can.
First, here is the xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="test2.MainWindow"
x:Name="Window"
Title="MainWindow">
<Grid x:Name="LayoutRoot">
<ListView Margin="148,74,0,202" HorizontalAlignment="Left" ItemsSource="{Binding ListData}">
<ListView.View>
<GridView>
<GridViewColumn Header="Item" DisplayMemberBinding="{Binding ItemName}" />
<GridViewColumn Header="Options" Width="200" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="Reject" Height="20" Width="80" Margin="5,2" Command="{Binding Commands}" CommandParameter="RejectItem" />
<Button Content="Accept" Height="20" Width="80" Margin="5,2" Command="{Binding Commands}" CommandParameter="AcceptItem"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Content="Test button" Height="32" Margin="148,38,0,0" VerticalAlignment="Top" Command="{Binding Commands}" CommandParameter="test" HorizontalAlignment="Left" Width="126"/>
</Grid>
</Window>
And the code behind...
using
System.Windows;
namespace
test2
{
publicpartialclassMainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
DataContext =
newMainWindowView();
}
}
}
And here is the view model...
using System.Collections.Generic; using System.Windows.Input; using System.ComponentModel; using System; using System.Windows; namespace test2 { public class DummyData { public string ItemName { get; set; } //loads of other properties here } class MainWindowView : ICommand { public ICommand Commands { get; set; } private List<DummyData> _ListData; public List<DummyData> ListData { get { return _ListData; } set { _ListData = value; } } public MainWindowView() { Commands = this; ListData = new List<DummyData>() { new DummyData {ItemName = "Item one"}, new DummyData {ItemName = "Item two"}, new DummyData {ItemName = "Item three"}, }; } public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested += value; } } public void Execute(object parameter) { if (parameter != null) { switch (parameter.ToString().ToLower()) { case "rejectitem": // How do I know which item this relates to? break; case "acceptitem": // How do I know which item this relates to? break; case "test": MessageBox.Show("yey"); break; } } } } }
You will also see a button. The button command triggers perfectly, but the buttons within the list item template do not. And even if they did, how do I know which item it relates to? When I click a sub-button, the listbox row itself is not selected, so I can't bind to SelectedItem..
Can some MVVM guru please explain what I'm doing wrong or missunderstanding, and possibly post some example code?
Many thanks in advance,
Pedro