0
我正在使用Telerik控件庫構建.Net WPF應用程序。
我的儀表板有幾個我實現的菜單。
我正在嘗試使用MVVM模型。如何將命令綁定到XAML中的WPF Telerik radMenuItem?
類定義爲我的菜單項(在MVVM中)看起來是這樣的:
public class MenuItemVM : MenuVM
{
public enum MenuItemTypes : uint
{
Link,
SpecialLink,
TopLevel,
TopLevelWithDropDown,
TopLevelImage,
TopLevelSection,
Title,
Item,
Footer,
Separator,
Paragraph,
Gallery,
Image
};
public string Name { get; set; }
public string Content { get; set; }
public MenuItemTypes Type { get; set; }
private bool isSeparator = false;
public bool IsSeparator
{
get { return isSeparator; }
set { isSeparator = value;}
}
public ICommand Command { get; set; }
}
在XAML菜單的定義是這樣的:
telerik:RadMenu Grid.Column="0"
Name="MainMenu"
Orientation="Vertical"
Margin="10,30,0,20"
Style="{StaticResource MainMenuStyle}"
Height="1200"
ItemsSource="{StaticResource MainMenuItemsSource}"
ItemContainerStyleSelector="{StaticResource MainMenuItemStyleSelector}"
ItemContainerStyle="{x:Null}"
我的ViewModel是在Windows.Resources XAML中定義的:
<viewModel:MenuVM x:Key="MainMenuItemsSource">
<viewModel:MenuItemVM Name="Test1" Content="Do Test 1" Type="Item" Command="??"/>
<viewModel:MenuItemVM Name="Test2" Content="Do Test 2" Type="Item" Command="??"/>
<viewModel:MenuItemVM Name="Test3" Content="Do Test 3" Type="Item" Command="??"/>
</viewModel:MenuVM>
我正在使用「Style」和「ControlTemplate」來定義菜單控件的佈局。
我有一個簡單的命令,我試圖綁定到一個菜單項。
namespace SampleApp.Commands
{
public class CommandTest : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
MessageBox.Show("Command Clicked!!!");
}
}
}
菜單正確顯示。
我想不出如何將命令綁定到菜單定義中的菜單項。
您是否嘗試過使用公共CommandTest命令{get;組; }'而不是'public ICommand Command {get;組; }',並使用'Command =「{綁定命令}」綁定。爲了您的清晰,我建議使用「Command」以外的名稱。例如,'public ICommand ButtonCommand {get;組; }'。 –
您是否嘗試過我的建議或發生了什麼? – mm8