获取附加属性的值而不是设置它

发布于 2024-11-29 01:41:57 字数 1161 浏览 0 评论 0原文

我有一些代码来设置文本框的焦点属性,但我实际上想要的是找出文本框当前是否具有键盘焦点,我需要从我的视图模型中确定这一点

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)
    );

    public static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var uie = (UIElement)d;
        if ((bool)e.NewValue)
        {
            uie.Focus();
        }
    }
}

xaml 是

<TextBox Text="{Binding Path=ClientCode}" c:FocusExtension.IsFocused="{Binding IsClientCodeFocused}" />

代码的来源

I have some code to set the focused property of a text box, but what i'm actually after is finding out if the text box currently has the keyboard focus, I need to determine this from my view model

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)
    );

    public static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var uie = (UIElement)d;
        if ((bool)e.NewValue)
        {
            uie.Focus();
        }
    }
}

And the xaml is

<TextBox Text="{Binding Path=ClientCode}" c:FocusExtension.IsFocused="{Binding IsClientCodeFocused}" />

source of code

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

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

发布评论

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

评论(3

淡写薰衣草的香 2024-12-06 01:41:57

have you seen the FocusManager? you can get/set focus using this object.

浮生面具三千个 2024-12-06 01:41:57

编辑

根据下面的注释,下面是一个附加属性的示例,该属性挂钩事件并更新绑定源。我将在我知道您需要进行修改的地方添加评论。希望它能为您指明正确的方向

public class TextBoxHelper
{
    // I excluded the generic stuff, but the property is called 
    // EnterUpdatesSource and it makes a TextBox update it's source
    // whenever the Enter key is pressed

    // Property Changed Event - You have this in your class above
    private static void EnterUpdatesTextSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        UIElement sender = obj as UIElement;
        if (obj != null)
        {
            // In my case, the True/False value just determined a behavior,
            // so toggling true/false added/removed an event.

            // Since you want your events to be on at all times, you'll either
            // want to have two AttachedProperties (one to tell the control
            // that it should be tracking the current focused state, and 
            // another for binding the actual focused state), or you'll want 
            // to find a way to only add the EventHandler when the 
            // AttachedProperty is first added and not toggle it on/off as focus 
            // changes or add it repeatedly whenever this value is set to true

            // You can use the GotFocus and LostFocus Events
            if ((bool)e.NewValue == true)
            {
                sender.PreviewKeyDown += new KeyEventHandler(OnPreviewKeyDownUpdateSourceIfEnter);
            }
            else
            {
                sender.PreviewKeyDown -= OnPreviewKeyDownUpdateSourceIfEnter;
            }
        }
    }

    // This is the EventHandler
    static void OnPreviewKeyDownUpdateSourceIfEnter(object sender, KeyEventArgs e)
    {
        // You won't need this
        if (e.Key == Key.Enter)
        {
            // or this
            if (GetEnterUpdatesTextSource((DependencyObject)sender))
            {
                // But you'll want to take this bit and modify it so it actually 
                // provides a value to the Source based on UIElement.IsFocused
                UIElement obj = sender as UIElement;

                // If you go with two AttachedProperties, this binding should 
                // point to the property that contains the IsFocused value
                BindingExpression textBinding = BindingOperations.GetBindingExpression(
                    obj, TextBox.TextProperty);

                // I know you can specify a value for a binding source, but
                // I can't remember the exact syntax for it right now
                if (textBinding != null)
                    textBinding.UpdateSource();
            }
        }
    }

可能有更好的方法来完成您想要做的事情,但如果没有,那么我希望这提供了一个良好的起点:)

Edit

Based on the comments below, here's an example of an attached property that hooks up an event and updates the source of a binding. I'll add comments where I know you'll need to make modifications. Hopefully it will point you in the right direction

public class TextBoxHelper
{
    // I excluded the generic stuff, but the property is called 
    // EnterUpdatesSource and it makes a TextBox update it's source
    // whenever the Enter key is pressed

    // Property Changed Event - You have this in your class above
    private static void EnterUpdatesTextSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        UIElement sender = obj as UIElement;
        if (obj != null)
        {
            // In my case, the True/False value just determined a behavior,
            // so toggling true/false added/removed an event.

            // Since you want your events to be on at all times, you'll either
            // want to have two AttachedProperties (one to tell the control
            // that it should be tracking the current focused state, and 
            // another for binding the actual focused state), or you'll want 
            // to find a way to only add the EventHandler when the 
            // AttachedProperty is first added and not toggle it on/off as focus 
            // changes or add it repeatedly whenever this value is set to true

            // You can use the GotFocus and LostFocus Events
            if ((bool)e.NewValue == true)
            {
                sender.PreviewKeyDown += new KeyEventHandler(OnPreviewKeyDownUpdateSourceIfEnter);
            }
            else
            {
                sender.PreviewKeyDown -= OnPreviewKeyDownUpdateSourceIfEnter;
            }
        }
    }

    // This is the EventHandler
    static void OnPreviewKeyDownUpdateSourceIfEnter(object sender, KeyEventArgs e)
    {
        // You won't need this
        if (e.Key == Key.Enter)
        {
            // or this
            if (GetEnterUpdatesTextSource((DependencyObject)sender))
            {
                // But you'll want to take this bit and modify it so it actually 
                // provides a value to the Source based on UIElement.IsFocused
                UIElement obj = sender as UIElement;

                // If you go with two AttachedProperties, this binding should 
                // point to the property that contains the IsFocused value
                BindingExpression textBinding = BindingOperations.GetBindingExpression(
                    obj, TextBox.TextProperty);

                // I know you can specify a value for a binding source, but
                // I can't remember the exact syntax for it right now
                if (textBinding != null)
                    textBinding.UpdateSource();
            }
        }
    }

There might be a better way of accomplishing what you're trying to do, but if not then I hope this provides a good starting point :)

彼岸花ソ最美的依靠 2024-12-06 01:41:57

OnIsFocusedPropertyChanged 处理程序中,您需要获取对正在设置的控件的引用并订阅其 FocusChanged 事件,您可以在其中重新设置依赖项 pproperty 。确保在 XAML 中将绑定模式设置为 TwoWay

in your OnIsFocusedPropertyChanged handler, you need to get a reference to the control that it is being set on and subscribe to its FocusChanged event, where you can re-set the dependency pproperty. Make sure in your XAML you set the binding mode to TwoWay

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