【发布时间】:2013-10-28 22:58:36
【问题描述】:
我有一个程序,其中一个按钮只有在相应的 userControl 具有焦点时才应该处于活动状态。
我正在使用 MVVM 灯,并得到一个实现 ICommand 接口的命令。
我尝试过使用 Keyboard.FocusedElement,但这没有返回任何内容。
这是命令的代码(请注意,它现在只是返回 true 以使其正常工作,这当然是我要修复的问题):
class AddItemToNodeCommand<T> : ICommand
{
public bool CanExecute(object parameter)
{
Debug.WriteLine("fokuselement er: " + Keyboard.FocusedElement);
return true;
// throw new NotImplementedException();
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
Debug.WriteLine("Parameter er: " + parameter);
Debug.WriteLine("fokuselement er: " + Keyboard.FocusedElement);
//throw new NotImplementedException();
}
}
从视图模型:
public ICommand AddItemToNodeCommand { get; private set; }
AddItemToNodeCommand = new AddItemToNodeCommand<object>();
最后是一些 XAML:
<RibbonButton SmallImageSource="../Images/whatever.png" Label="Attribute" Command="{Binding AddItemToNodeCommand}" CommandParameter="Attribute"/>
我还没有发布 userControl 的 xaml,但我的想法是当 userControl 有焦点时,CanExecute 应该是真的。我认为它可以与 Keyboard.FokusedElement 一起使用,但我错了。我能做什么?
提前谢谢你。
【问题讨论】:
-
你的按钮是你想要检查焦点的用户控件的子控件吗?
-
不,该按钮是另一个 userControl(功能区控件)的一部分
-
从功能的角度来看,我认为只要将焦点从 userControl 移开,按钮就会被禁用。所以我不认为按钮在功能上仍然有用。如果我没有理解您的问题,请纠正我。
-
嗨帕拉克。是的,我明白你的意思。即使那时我所做的可能行不通,但我仍然不明白为什么 Keyboard.Focus 没有返回任何内容。我想要实现的是,当我单击 userControl 时,该按钮应该可用,直到焦点移到其他位置(如另一个 userControl)
标签: wpf mvvm mvvm-light icommand canexecute