2011-08-12 66 views
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是

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

+1

通過此附加屬性,您可以綁定到布爾型DependencyProperty或INPC屬性,以便在屬性更改時設置焦點。 **你在哪裏尋找它的焦點?** – Will

+0

@將從我的viewmodel – Rob

+0

您的ViewModel不在乎什麼是重點。這是View的責任。 [你的問題是無效的。](http://i.stack.imgur.com/PBUhJ.jpg)(抱歉,不想花時間從「旗」改爲「問題」)。我認爲你的真正問題應該是你正在努力完成的任務:你對什麼是和沒有關注的控制,以及如何在不違反MVVM的情況下做到這一點。 – Will

回答

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

有可能是完成你想要做的更好的辦法,但如果沒有的話,我希望這提供了一個很好的出發點:)

+0

我不確定這是否可以從我的viewmodel – Rob

+0

@Rob訪問您的ViewModel的IsClientCodeFocused是否包含焦點與否?等待我想我看到你在做什麼....你會想要掛鉤一個'FocusChanged'事件來更新你的AttachedProperty中的綁定源,當它被設置爲一個值。 – Rachel

+0

聽起來不錯!不幸的是,我不知道該怎麼做,應該在FocusExtension類中,還是在視圖中,甚至是在視圖模型中? – Rob

1

你有看過FocusManager嗎?您可以使用此對象獲取/設置焦點。

0

在您的OnIsFocusedPropertyChanged處理程序中,您需要獲取對其設置的控件的引用,並訂閱其FocusChanged事件,您可以在其中重新設置依賴關係屬性。確保在您的XAML中將綁定模式設置爲TwoWay