清除 MenuItem,使用清除命令

发布于 2024-10-26 01:07:56 字数 286 浏览 8 评论 0原文

你好 我是 wpf 的“非常”初学者 我正在尝试创建一个菜单项“清除”,它应该清除聚焦文本框中的文本, 实际上,我找不到一个内置命令可以完成类似(复制、粘贴、剪切等工作)的工作,

是否有内置命令,或者我是否必须创建一个自定义路由命令,如果是的话 我尝试过但失败了,需要一些想法

我已经制作了 ClearCommandExecuted 逻辑,但问题出在“CanExecute” 我尝试在那里访问 Keyboard.FocusedElement ,但失败了,因为焦点元素是单击时本身的菜单项!!!!

请帮忙 谢谢

hi
i'm a "very" beginner in wpf
i'm trying to make a menu item "Clear", it should clear the text in the focused text box,
actually i could not find a built in command that does the job like (copy,paste,cut..etc)

is there one built in or do i have to make a custom routed command, and if so
i've tried but failed, and need ideas

i've made the ClearCommandExecuted logic, but the problem is with "CanExecute"
i tried to access the Keyboard.FocusedElement there, but failed because the focused element is the menu item it self when it's clicked !!!!

please help
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2024-11-02 01:07:56

您需要使用传递到 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;
        } 
    }

我希望这足以让您朝着正确的方向前进......

You need to use one of the arguments passed into your 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;
        } 
    }

I hope this is enough to get you headed in the right direction...

我要还你自由 2024-11-02 01:07:56

尝试使用 FocusManager 类。当您的文本框失去键盘焦点时,如果它位于焦点范围内,它仍然具有逻辑焦点。 WPF 中默认为焦点范围的类有 Window、MenuItem、ToolBar 和 ContextMenu。

因此,使用它会给你结果 -

FocusManager.GetFocusedElement(winodw1); //Name of the window

有关更多详细信息,请阅读此 - http:// /msdn.microsoft.com/en-us/library/aa969768.aspx

Try to use the FocusManager class. When your TextBox has lost Keyboard Focus, it still has Logical Focus, if it is inside the Focus Scope. Classes in WPF which are focus scopes by default are Window, MenuItem, ToolBar, and ContextMenu.

So using this will give you the result -

FocusManager.GetFocusedElement(winodw1); //Name of the window

For more details, read this - http://msdn.microsoft.com/en-us/library/aa969768.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文