gpt4 book ai didi

wpf - 通过样式 setter 设置上下文菜单时,PlacementTarget 属性为空

转载 作者:行者123 更新时间:2023-12-04 21:49:56 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个 View (ListView) 和一个 View 模型。在 View 模型中,我有两个属性:第一个是项目列表,第二个是命令。我想在 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 为空。在调用命令之前,我需要以某种方式将单击的 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>

关于wpf - 通过样式 setter 设置上下文菜单时,PlacementTarget 属性为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6812537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com