[英]Uwp attached property not firing a property changed callback
我定義了一個自定義附加屬性:
public static IList<Object> GetBindings(DependencyObject obj)
{
return (IList<Object>)obj.GetValue(BindingsProperty);
}
public static void SetBindings(DependencyObject obj, IList<Object> value)
{
obj.SetValue(BindingsProperty, value);
}
// Using a DependencyProperty as the backing store for Bindings. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BindingsProperty =
DependencyProperty.RegisterAttached("Bindings", typeof(IList<Object>), typeof(KeyboardInput), new PropertyMetadata(new List<Object>(), OnBindingsChanged));
private static void OnBindingsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Debugger.Break();
}
Xaml:
<TextBox x:Name="textbox"
PlaceholderText="Add a comment...">
<app:KeyboardInput.Bindings>
<app:KeyBinding />
</app:KeyboardInput.Bindings>
</TextBox>
和方法,用於偵聽屬性更改( OnBindingsChanged
)。 將默認值( new List<object>()
)分配給屬性時,為什么不觸發? 在xaml中構造目標對象后,如何訪問它?
該事件不會觸發,因為您已為屬性指定了new List<object>
的默認值,因此在XAML中設置綁定時應如下所示:
<app:KeyboardInput.Bindings>
<app:KeyBinding />
</app:KeyboardInput.Bindings>
您只是將一個項目添加到現有列表中而不會更改屬性的實際值。 我建議將其替換為ObservableCollection,然后偵聽每次添加或刪除項目時都會觸發的CollectionChanged事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.