我如何才能訪問相關的MenuItem
?它已經實時創建,所以我不能僅僅通過xaml文件中的名稱來使用它。如何從CanExecute處理程序中獲取MenuItem?
private void menuItem_canExecute(object sender, CanExecuteRoutedEventArgs e)
{
var snd = sender; // This is the main window
var orgSource = e.OriginalSource; // This is a RichTextBox;
var src = e.Source; // This is a UserControl
// I think I must use the Command, but how?
RoutedCommand routedCommand = e.Command as RoutedCommand;
}
您可以在菜單項的'CommandParameter'綁定到菜單項實例,比如'CommandParameter =「{綁定的RelativeSource = {的RelativeSource自} }並且可以通過CanExecuteRoutedEventArgs的'Parameter'屬性來訪問它。 – Clemens
爲什麼你需要訪問'MenuItem'?也許有更好的方法來完成你想要做的事情。 – StillLearnin
@Clemens:這是一個可行的解決方案,請將您的評論轉換爲答案。 – Pollitzer