通过样式设置器设置contextmenu时,PlacementTarget属性为null

时间:2011-07-25 06:40:27

标签: wpf mvvm contextmenu

在我的应用程序中,我有一个视图(ListView)和一个视图模型。在内部视图模型中,我有2个属性:第一个是项目列表,第二个是命令。我想在ListView中显示项目(来自第一个属性)。另外,我希望每个菜单都有一个上下文菜单,点击它会激活一个命令(来自第二个属性)。

以下是我的视图模型的代码:

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"
            }));
        }
    }

现在是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>

问题:在我实际打开上下文菜单之前,上下文菜单的PlacementTarget为null。我需要以某种方式接收被点击的ListViewItem的数据上下文到命令的“CanExecute”,在被调用的命令之前 - 我真的希望在XAML中创建所有内容,而无需处理后面代码中的任何回调。

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

如果您正在寻找ListViewItem的DataContext,您可以这样做:

CommandParameter="{Binding}"

修改 - 这是我尝试的内容:

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

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