WPF 的新手......正在阅读这个WPF 路由命令,每个选项卡都有绑定,并且即将开始工作。
MenuItem 被禁用,直到我的 RuleTab (tabitem) 被选中,而不是弹出我的查找对话框,它在菜单上显示 System.Windows.Input.CommandBinding。我究竟做错了什么?
XAML:
<MenuItem Header="_Find..." IsEnabled="{Binding ElementName=RuleTab, Path=IsSelected}" >
<CommandBinding Command="Find" Executed="ExecuteFind" CanExecute="Find_CanExecute" ></CommandBinding>
</MenuItem>
代码隐藏:
private void ExecuteFind(object sender, ExecutedRoutedEventArgs e)
{
// Initiate FindDialog
FindDialog dlg = new FindDialog(this.RuleText);
// Configure the dialog box
dlg.Owner = this;
dlg.TextFound += new TextFoundEventHandler(dlg_TextFound);
// Open the dialog box modally
dlg.Show();
}
void dlg_TextFound(object sender, EventArgs e)
{
// Get the find dialog box that raised the event
FindDialog dlg = (FindDialog)sender;
// Get find results and select found text
this.RuleText.Select(dlg.Index, dlg.Length);
this.RuleText.Focus();
}
private void Find_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = RuleTab.IsSelected;
}
任何建议将不胜感激!
弄清楚了!感谢那些回应。我所要做的就是将我的命令绑定移动到:
<Window.CommandBindings>
<CommandBinding Command="Find" Executed="ExecuteFind" CanExecute="Find_CanExecute" ></CommandBinding>
</Window.CommandBindings>
然后在我的 MenuItem 中引用 Command=Find。