[英]wpf mvvm light menu item click eventtocommand
我有以下問題。 我已經為菜單和菜單項創建了一個用戶控件:
用戶控件使用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()
{
}
}
}
嘗試在您的ViewModel中這樣做:
使用GalaSoft.MvvmLight; 使用GalaSoft.MvvmLight.Command;
namespace TestDashBoard.ViewModels
{
public class MenuItemViewModel : ViewModelBase
{
public RelayCommand _salesSetupClicked;
public RelayCommand SalesSetupClicked
{
get
{
if (_salesSetupClicked == null)
_salesSetupClicked = new RelayCommand(ShowSalesSetup);
return _salesSetupClicked;
};
private set;
}
public RelayCommand InvtSetupClicked
{
get;
private set;
}
public MenuItemViewModel()
{
SalesSetupClicked = new RelayCommand(() =>
{
ShowSalesSetup();
});
InvtSetupClicked = new RelayCommand(() =>
{
ShowInvtSetup();
});
}
private void ShowSalesSetup()
{
}
private void ShowInvtSetup()
{
}
}
}
嘗試將要發生的代碼放入某種構造方法中,而不是放入構造函數中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.