In my application, I have a view (ListView) and a view model. Inside the view model, I have 2 properties: first a list of elements, and the second a command. I want to display elements (from the first property) inside a ListView. In addition, I want to have a context menu for each, where when you click on it, a command is activated (from the second property).
Here is the code for my view model:
public class ViewModel { public IEnumerable Items { get { return ...;
Now part of XAML:
<ListView ItemsSource="{Binding Items}"> <ListView.Resources> <commanding:CommandReference x:Key="myCommand" Command="{Binding MyCommand}"/> </ListView.Resources> <ListView.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" /> </DataTemplate> </ListView.ItemTemplate> <ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu> <MenuItem Header="Remove from workspace" Command="{StaticResource myCommand}" CommandParameter="HERE I WANT TO PASS THE DATA CONTEXT OF THE ListViewItem" /> </ContextMenu> </Setter.Value> </Setter> </Style> </ListView.ItemContainerStyle> </ListView>
Problem: Until I actually open the context menu, the PlacementTarget of the context menu is null. I need to somehow get the context data of the ListViewItem click in the "CanExecute" command, before calling the command - and I really want to do everything in XAML without handling callbacks in the code.
Thanks in advance.
source share