需要有关 calibburn Message.Attach 当 TextBox 获得焦点时的帮助

发布于 2024-10-23 14:50:49 字数 1347 浏览 7 评论 0原文

我有一个 TextBox,我将焦点设置为使用绑定到视图模型属性的附加属性。附加属性调用“UIElement.Focus()”来设置焦点。问题是当文本框以这种方式接收焦点时,“GotFocus”事件不会触发。我正在使用 Caliburn.Micro 的 Message.Attach 来处理该事件。有什么想法吗?

这是文本框。

<TextBox x:Name="Test"
         Grid.Column="0"
         Text="{Binding Test, Converter={StaticResource TestToStringConverter}}"
         AttachedProperties:FocusExtension.IsFocused="{Binding IsTestFocused}"
         cal:Message.Attach="[Event GotFocus] = [Action OnGotFocus($eventargs)]; />

这是附加属性(在 SO 上找到)。

public static class FocusExtension
{
    public static bool GetIsFocused(DependencyObject obj)
    {
        return (bool) obj.GetValue(IsFocusedProperty);
    }

    public static void SetIsFocused(DependencyObject obj, bool value)
    {
        obj.SetValue(IsFocusedProperty, value);
    }

    public static readonly DependencyProperty IsFocusedProperty = 
        DependencyProperty.RegisterAttached("IsFocused", typeof (bool), typeof (FocusExtension),
                                            new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));

    private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var uie = (UIElement)d;

        if ((bool)e.NewValue)
        {
            uie.Focus();
        }
    }
}

I have a TextBox that I am setting the focus on using an attached property bound to a property of the view model. The attached property calls "UIElement.Focus()" to set the focus. The problem is when the TextBox receives focus in this manner the "GotFocus" event doesn't fire. I am using Caliburn.Micro's Message.Attach to handle the event. Any ideas?

Here is the TextBox.

<TextBox x:Name="Test"
         Grid.Column="0"
         Text="{Binding Test, Converter={StaticResource TestToStringConverter}}"
         AttachedProperties:FocusExtension.IsFocused="{Binding IsTestFocused}"
         cal:Message.Attach="[Event GotFocus] = [Action OnGotFocus($eventargs)]; />

Here is the Attached Property (found on SO).

public static class FocusExtension
{
    public static bool GetIsFocused(DependencyObject obj)
    {
        return (bool) obj.GetValue(IsFocusedProperty);
    }

    public static void SetIsFocused(DependencyObject obj, bool value)
    {
        obj.SetValue(IsFocusedProperty, value);
    }

    public static readonly DependencyProperty IsFocusedProperty = 
        DependencyProperty.RegisterAttached("IsFocused", typeof (bool), typeof (FocusExtension),
                                            new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));

    private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var uie = (UIElement)d;

        if ((bool)e.NewValue)
        {
            uie.Focus();
        }
    }
}

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

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

发布评论

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

评论(1

耳根太软 2024-10-30 14:50:49

我自己尝试过,并且能够复制该问题。我不太确定为什么会发生这种情况,它可能与用户控件(即视图)的生命周期有关。一种选择可能是扩展附加属性,以便它在调用 uie.Focus() 时调用视图模型上的动词。

动词的名称可以是 FocusExtension 附加属性的依赖属性,并且可以在视图中设置。

I have tried this myself, and am able to replicate the issue. I'm not quite sure why this happens, it may have something to do with the user control's (i.e. the views) lifecycle. One option could be to extend your attached property so that it invokes a verb on your view model at the point at which it calls uie.Focus().

The name of the verb could be a dependency property on your FocusExtension attached property, and could be set in the view.

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