【发布时间】:2013-10-08 19:19:11
【问题描述】:
我有以下问题。我为菜单和菜单项创建了一个用户控件:
用户控件启动,视图模型作为 Datacontext,视图模型的构造函数启动并调用模型中的 ReylayCommands。当我单击视图中的菜单项时。然后什么都没有发生。我错过了什么?
我的xml:
<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。
-
如果您的命令正常工作,您是否在调试模式下进行了检查?
-
使用 Button 可以正常工作。我尝试使用 FocusManger.IsFocusable="True"。不工作。我把它放在 Menu 或我试图点击的 MenuItem 中
-
是的。这没用。如果构造函数正在运行,我还检查了调试模式。
-
@user2063981 你能在出错的地方贴出代码吗?