Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When setting contextmenu via style setter, PlacementTarget property is null

In my application, I have a view (ListView) and a view model. Inside view model, I have 2 properties: first is a list of items, and the second is a command. I want to display items (from first property) inside ListView. In addition, I want to have for each one a context menu, where clicking on it will activate a command (from second property).

Here is a code of my view model:

public class ViewModel
{
    public IEnumerable Items
    {
        get
        {
            return ...;  //returns a collection of items
        }
    }

    public ICommand MyCommand //this is a command, I want to be able execute from context menu of each item
    {
        get
        {
            return new DelegateCommand(new Action<object>(delegate(object parameter)
            {
                //here code of the execution   
            }
            ), new Predicate<object>(delegate(object parameter)
            {
                //here code of "can execute"
            }));
        }
    }

Now the XAML part:

<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>

The problem: until I actually opening context menu, the PlacementTarget of the context menu is null. I need somehow to receive data context of the clicked ListViewItem into "CanExecute" of the command, BEFORE the command being called - and I truly wish to make everything in the XAML, without handling any callbacks in code behind.

Thank you in advance.

like image 351
Illidan Avatar asked Nov 14 '22 19:11

Illidan


1 Answers

If you are looking for ListViewItem's DataContext you can do this:

CommandParameter="{Binding}"

Edit - Here is what I tried:

public partial class MainWindow : Window
{
    private ObservableCollection<Person> list = new ObservableCollection<Person>();

    public MainWindow()
    {
        InitializeComponent();
        list.Add(new Person() { Name = "Test 1"});
        list.Add(new Person() { Name = "Test 2"});
        list.Add(new Person() { Name = "Test £"});
        list.Add(new Person() { Name = "Test 4"});
        this.DataContext = this;

    }

    public static ICommand MyCommand //this is a command, I want to be able execute from context menu of each item     
    {         
        get
        {
            return new DelegateCommand<Person>(
                a => Console.WriteLine(a.Name),
                a => true);
        }
    }

    public ObservableCollection<Person> Items
    {
        get
        {
            return this.list;
        }
    }
}

public class Person
{
    public string Name { get; set; }
}

And the xaml:

<Window x:Class="ListView1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ListView1="clr-namespace:ListView1" Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView ItemsSource="{Binding Items}">            
            <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="{x:Static ListView1:MainWindow.MyCommand}"  CommandParameter="{Binding}" />
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>
    </Grid>
</Window>
like image 61
anivas Avatar answered Dec 09 '22 10:12

anivas