Ill*_*dan 5 wpf contextmenu mvvm
在我的应用程序中,我有一个视图(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"
}));
}
}
Run Code Online (Sandbox Code Playgroud)
现在,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>
Run Code Online (Sandbox Code Playgroud)
问题:在我真正打开上下文菜单之前,上下文菜单的PlacementTarget为null。我需要某种方式来将单击的ListViewItem的数据上下文接收到命令的“ CanExecute”中,然后再调用该命令-我确实希望在XAML中进行所有操作,而无需处理后面的代码中的任何回调。
先感谢您。
如果您正在寻找 ListViewItem\'s,DataContext您可以这样做:
CommandParameter="{Binding}"
编辑-这是我尝试过的:
\n\npublic partial class MainWindow : Window\n{\n private ObservableCollection<Person> list = new ObservableCollection<Person>();\n\n public MainWindow()\n {\n InitializeComponent();\n list.Add(new Person() { Name = "Test 1"});\n list.Add(new Person() { Name = "Test 2"});\n list.Add(new Person() { Name = "Test \xc2\xa3"});\n list.Add(new Person() { Name = "Test 4"});\n this.DataContext = this;\n\n }\n\n public static ICommand MyCommand //this is a command, I want to be able execute from context menu of each item \n { \n get\n {\n return new DelegateCommand<Person>(\n a => Console.WriteLine(a.Name),\n a => true);\n }\n }\n\n public ObservableCollection<Person> Items\n {\n get\n {\n return this.list;\n }\n }\n}\n\npublic class Person\n{\n public string Name { get; set; }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n和 xaml:
\n\n<Window x:Class="ListView1.MainWindow"\n xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"\n xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ListView1="clr-namespace:ListView1" Title="MainWindow" Height="350" Width="525">\n <Grid>\n <ListView ItemsSource="{Binding Items}"> \n <ListView.ItemTemplate>\n <DataTemplate>\n <TextBlock Text="{Binding Name}" />\n </DataTemplate>\n </ListView.ItemTemplate>\n <ListView.ItemContainerStyle>\n <Style TargetType="{x:Type ListViewItem}">\n <Setter Property="ContextMenu">\n <Setter.Value>\n <ContextMenu>\n <MenuItem Header="Remove from workspace" Command="{x:Static ListView1:MainWindow.MyCommand}" CommandParameter="{Binding}" />\n </ContextMenu>\n </Setter.Value>\n </Setter>\n </Style>\n </ListView.ItemContainerStyle>\n </ListView>\n </Grid>\n</Window>\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3892 次 |
| 最近记录: |