2016-04-18 25 views
1

.NET 3.5のコントロールの1つにAttached Propertyを使用しようとしました。コントロールは特定の操作の後で焦点を合わせる必要があるという考えです。このプロパティをViewModelの値にバインドして、必要に応じてプロパティを変更できるようにしました(つまり、ViewModelの値を変更したときに、Attachedプロパティの値が更新されます)。 XAMLで添付されたプロパティへのデータのバインド

プロパティ添付

class FocusProperty : DependencyObject 
{ 
    public static DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached ("IsFocused", 
                      typeof (bool), 
                      typeof (FocusProperty), 
                      new UIPropertyMetadata (false, OnIsFocusedChanged)); 

    public static bool GetIsFocused (DependencyObject DObject) 
    { 
     return (bool)DObject.GetValue (IsFocusedProperty); 
    } 

    public static void SetIsFocused (DependencyObject DObject, bool Value) 
    { 
     DObject.SetValue (IsFocusedProperty, Value); 
    } 

    public static void OnIsFocusedChanged (DependencyObject DObject, DependencyPropertyChangedEventArgs Args) 
    { 
     UIElement Control = DObject as UIElement; 
     bool NewValue = (bool)Args.NewValue; 
     bool OldValue = (bool)Args.OldValue; 

     // REMOVE TODO 
     System.Windows.MessageBox.Show ("OI"); 

     if (NewValue && !OldValue && !Control.IsFocused) 
     { 
     Control.Focus(); 
     } 
    } 
} 

、これは、それが使用されている方法です:

IsListFocusedはViewModelににブールプロパティです
a:FocusProperty.IsFocused="{Binding Path=IsListFocused} 

public bool IsListFocused 
{ 
    get { return isListFocused_; } 
    set 
    { 
     isListFocused_ = value; 
     OnPropertyChanged ("IsFocused"); 
    } 
} 

それは動作しません - IsListFocused MessageBoxが期待通りに表示されません。

私はトピックに関するヘルプを探していましたが、Attached Propertiesはあまり使われていないようで、サポートの方法はあまりありません。

私の質問は:上記のスニペットが期待どおりに機能しないのはなぜですか?あなたのビューモデルでIsListFocusedプロパティのセッターで

答えて

2

OnPropertyChanged("IsListFocused"); 
+0

Welpによって

OnPropertyChanged("IsFocused"); 

を交換してください。もっと見えてはいかがでしたか?さて、助けてくれてありがとう。 – MKII

関連する問題