我有以下問題。我爲菜單和菜單項創建了一個用戶控件: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