用户控制处焦点丢失 [wpf]

发布于 2024-11-30 10:08:35 字数 128 浏览 0 评论 0原文

FocusLost 事件在 WPF 组件上正常工作 对于用户控件来说,情况有所不同:

如果单击或聚焦用户控件中的任何控件,则立即触发用户控件的 FocusLost !我怎样才能阻止它呢?

我无法解决这个问题:(

FocusLost event is working on WPF's components properly
For User-Controls it is different:

if any control in the User-Control is clicked or focused, the User-Control's FocusLost is fired immediately ! How can I hinder it ?

I could not solve this problem :(

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

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

发布评论

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

评论(2

阪姬 2024-12-07 10:08:36

您可以检查UserControl 上的IsKeyboardFocusWithin 来确定UserControl 或其任何子项是否具有KeyBoard 焦点。您还可以使用事件 IsKeyboardFocusWithinChanged ,只需检查 e.OldValue 是否为 truee.NewValue > 在事件处理程序中为 false

编辑
下面是一个示例,说明当 IsKeyboardFocusWithin 变为 false 时,如何使 UserControl 引发路由事件 (UserControlLostFocus)。

一样使用

<my:UserControl1 UserControlLostFocus="userControl11_UserControlLostFocus"
                 ../>

示例UserControl

public partial class UserControl1 : UserControl
{
    public static readonly RoutedEvent UserControlLostFocusEvent =
        EventManager.RegisterRoutedEvent("UserControlLostFocus",
                                         RoutingStrategy.Bubble,
                                         typeof(RoutedEventHandler),
                                         typeof(UserControl1));
    public event RoutedEventHandler UserControlLostFocus
    {
        add { AddHandler(UserControlLostFocusEvent, value); }
        remove { RemoveHandler(UserControlLostFocusEvent, value); }
    }

    public UserControl1()
    {
        InitializeComponent();
        this.IsKeyboardFocusWithinChanged += UserControl1_IsKeyboardFocusWithinChanged;
    }

    void UserControl1_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if ((bool)e.OldValue == true && (bool)e.NewValue == false)
        {
            RaiseEvent(new RoutedEventArgs(UserControl1.UserControlLostFocusEvent, this));
        }
    }
}

You can check IsKeyboardFocusWithin on the UserControl to determine if the UserControl or any of its children has the KeyBoard focus or not. There is also the event IsKeyboardFocusWithinChanged which you can use, just check if e.OldValue is true and e.NewValue is false in the event handler.

Edit.
Here is an example of how you could make the UserControl raise a routed event (UserControlLostFocus) when IsKeyboardFocusWithin turns to false.

Useable like

<my:UserControl1 UserControlLostFocus="userControl11_UserControlLostFocus"
                 ../>

Sample UserControl

public partial class UserControl1 : UserControl
{
    public static readonly RoutedEvent UserControlLostFocusEvent =
        EventManager.RegisterRoutedEvent("UserControlLostFocus",
                                         RoutingStrategy.Bubble,
                                         typeof(RoutedEventHandler),
                                         typeof(UserControl1));
    public event RoutedEventHandler UserControlLostFocus
    {
        add { AddHandler(UserControlLostFocusEvent, value); }
        remove { RemoveHandler(UserControlLostFocusEvent, value); }
    }

    public UserControl1()
    {
        InitializeComponent();
        this.IsKeyboardFocusWithinChanged += UserControl1_IsKeyboardFocusWithinChanged;
    }

    void UserControl1_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if ((bool)e.OldValue == true && (bool)e.NewValue == false)
        {
            RaiseEvent(new RoutedEventArgs(UserControl1.UserControlLostFocusEvent, this));
        }
    }
}
木落 2024-12-07 10:08:36

如果您不希望在焦点丢失时触发任何内容,可以将其添加到 XAML 用户控件后面的源 .CS 文件中:

protected override void OnLostFocus(RoutedEventArgs e)
{
    e.Handled = true;
}

If you dont want anything to fire on focus being lost you can add this to the source .CS file behind the XAML User Control:

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