[英]Data binding to an attached property
我試圖對.NET 3.5中的控件之一使用Attached Property
。 這個想法是控件需要在特定操作之后集中。 我想將此屬性綁定到ViewModel的值,以便可以根據需要更改該屬性(即,當我更改ViewModel的值時,它將更新Attached Property中的值)。
附屬財產
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 ();
}
}
}
在XAML中,其用法如下:
a:FocusProperty.IsFocused="{Binding Path=IsListFocused}
其中IsListFocused
是ViewModel的bool屬性:
public bool IsListFocused
{
get { return isListFocused_; }
set
{
isListFocused_ = value;
OnPropertyChanged ("IsFocused");
}
}
但是它不起作用-更改IsListFocused后,MessageBox不會按預期顯示。
我一直在尋求有關該主題的幫助,但似乎附加屬性用處不大,因此,支持的方式也很少。
我的問題是: 為什么以上代碼片段無法按預期運行?
在視圖模型的IsListFocused
屬性設置器中,替換
OnPropertyChanged("IsFocused");
通過
OnPropertyChanged("IsListFocused");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.