我有以下问题。我为菜单和菜单项创建了一个用户控件:wpf mvvm light menu item click eventtocommand
usercontrols启动将viewmodel作为Datacontext和视图模型的构造函数激发并调用模型中的ReylayCommands。当我点击视图中的菜单项时。然后发生事情。我错过了什么?
我的XAML:
<UserControl x:Class="TestDashBoard.Views.MenuItemView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:prop="clr-namespace:TestDashBoard.Properties"
xmlns:i="clr namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
mc:Ignorable="d" >
<Menu IsMainMenu="True">
<MenuItem Header="{x:Static prop:Resources.Setup}">
<MenuItem x:Name="salesSetup" Header="{x:Static prop:Resources.SaleSetup}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding SalesSetupClicked, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</MenuItem>
</MenuItem>
</Menu>
</UserControl>
我的视图模型类:
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
namespace TestDashBoard.ViewModels
{
public class MenuItemViewModel : ViewModelBase
{
public RelayCommand SalesSetupClicked
{
get;
private set;
}
public RelayCommand InvtSetupClicked
{
get;
private set;
}
public MenuItemViewModel()
{
SalesSetupClicked = new RelayCommand(() =>
{
ShowSalesSetup();
});
InvtSetupClicked = new RelayCommand(() =>
{
ShowInvtSetup();
});
}
private void ShowSalesSetup()
{
}
private void ShowInvtSetup()
{
}
}
}
请问您可以检查这个命令是否可以正常使用Button?如果是,则表示将FocusManager.IsFocusScope =“True”属性设置为Menu。 – Ravuthasamy
您是否在调试模式下检查,如果您的命令正在工作? – Sasha
它工作正常与按钮。我试图使用FocusManger.IsFocusable =“True”。不起作用。要么我把它放在Menu或MenuItem中,我试图点击 – user2063981