[英]Clear MenuItem, with clear command
嗨,我是wpf中的“非常”初學者,我正在嘗試使菜單項“清除”,它應該清除焦點文本框中的文本,實際上我找不到能完成以下工作的內置命令(復制,粘貼,剪切等)
有內置的命令還是我必須做一個自定義的路由命令,如果是,我已經嘗試過但失敗了,需要思路
我已經執行了ClearCommandExecuted邏輯,但是問題出在“ CanExecute”上,我試圖在那兒訪問Keyboard.FocusedElement,但是失敗了,因為焦點元素是單擊時它自身的菜單項!
請幫助謝謝
您需要使用傳遞到CanExecuteQuery中的參數之一:
private void ClearCommandBindingCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
// e.Source is the element that is active,
if (e.Source is TextBox) // and whatever other logic you need.
{
e.CanExecute = true;
e.Handled = true;
}
}
private void ClearCommandBindingExecuted(object sender, ExecutedRoutedEventArgs e)
{
var textBox = e.Source as TextBox;
if (textBox != null)
{
textBox.Clear();
e.Handled = true;
}
}
我希望這足以使您朝正確的方向前進...
嘗試使用FocusManager
類。 當您的文本框失去鍵盤焦點時,如果它位於焦點范圍內,則它仍然具有邏輯焦點。 WPF中作為焦點范圍的類在默認情況下是Window,MenuItem,ToolBar和ContextMenu。
因此,使用它會給您結果-
FocusManager.GetFocusedElement(winodw1); //Name of the window
有關更多詳細信息,請閱讀此內容-http://msdn.microsoft.com/zh-cn/library/aa969768.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.