我的XAML中有以下代码:
<Menu x:Name="MainMenu" VerticalAlignment="Top" DockPanel.Dock="Top" Padding="0,3">
<Menu.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Text}" Icon="{Binding Icon}" ToolTip="{Binding Tooltip}" Command="{Binding Path=ClickCommand}" HorizontalAlignment="Left">
</MenuItem>
</DataTemplate>
</Menu.ItemTemplate>
</Menu>
然后我绑定ItemsSource属性并用插件中的菜单项填充它。
不是我认为它是相关的,这里有一些代码:
MainMenu.ItemsSource = Global.MainMenu; //You can assume the Global class is exactly what it looks like, a shared class holding many of the programs settings and stores.
ApplicationMenuItem mappingMenu = new ApplicationMenuItem();
mappingMenu.Text = "Edit Mapping";
mappingMenu.Clicked += (se, ev) => { MainFrame.Navigate(new Pages.DeviceMapping()); };
Global.MainMenu.Add(mappingMenu);
-
public class ApplicationMenuItem
{
public ImageSource Icon { get; set; }
public string Text { get; set; }
public string Tooltip { get; set; }
public bool Enabled { get; set; }
public ICollection<ApplicationMenuItem> Items { get; set; }
public EventHandler Clicked {get;set;}
public void Click(object sender, EventArgs e)
{
if (Clicked!=null)
Clicked(this, e);
}
private ICommand _ClickCommand;
public ICommand ClickCommand
{
get
{
if (_ClickCommand == null)
{
_ClickCommand = new RelayCommand(param => this.Click(this, EventArgs.Empty));
}
return _ClickCommand;
}
}
public ApplicationMenuItem(string Text)
{
this.Text = Text;
Items = new List<ApplicationMenuItem>();
}
public ApplicationMenuItem()
{
Items = new List<ApplicationMenuItem>();
}
}
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameters)
{
return _canExecute == null ? true : _canExecute(parameters);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameters)
{
_execute(parameters);
}
#endregion // ICommand Members
}
问题是项目没有以我期望的方式显示,它们似乎比MenuItem
控件内的正常Menu
宽得多,并且似乎被包裹在另一个控件中导致双边界。
我哪里出错了,我没有正确使用菜单的绑定吗?