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

时间:2011-08-12 14:15:38

标签: c# wpf xaml mvvm

我有一些代码来设置文本框的聚焦属性,但我实际上是在查看当前的文本框是否键盘焦点,我需要从中确定我的观点模型

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}" />

来源code

3 个答案:

答案 0 :(得分:1)

你看过FocusManager了吗?你可以使用这个对象获得/设置焦点。

答案 1 :(得分:0)

修改

根据下面的评论,这里是一个附加属性的示例,它挂接事件并更新绑定的源。我会在我知道您需要进行修改的地方添加评论。希望它会指出你正确的方向

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

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

答案 2 :(得分:0)

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