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

这是我的 View 模型的代码:

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中进行所有操作,而不处理后面的代码中的任何回调。

先感谢您。

最佳答案

如果您正在寻找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>

09-25 17:20
查看更多