我在ClassA中有一個DependencyProperty。我從ClassA中派生出另一個ClassB。在ClassA中更新或更改此屬性的值時,如何在派生ClassB中通知或反映此類屬性,而無需在ClassA中添加任何其他代碼?WPF中的基類DependencyProperty值變化
3
A
回答
4
通過通過DependencyProperty.OverrideMetadata在派生類中註冊了一個ClassB的PropertyChangedCallback:
class ClassB
{
static ClassB()
{
ClassA.SomeValueProperty.OverrideMetadata(
typeof(ClassB), new PropertyMetadata(SomeValuePropertyChanged);
}
private static SomeValuePropertyChanged(
DependencyObject o, DependencyPropertyChangedArgs e)
{
...
}
}
3
如果你想在你可以添加其他所有者兩個類被提出的財產變化。
using System.Windows;
namespace dp
{
public class ClassA : DependencyObject
{
public string TestProperty
{
get { return (string)GetValue(TestPropertyProperty); }
set { SetValue(TestPropertyProperty, value); }
}
public static readonly DependencyProperty TestPropertyProperty =
DependencyProperty.Register("TestProperty", typeof(string), typeof(ClassA), new PropertyMetadata(null, new PropertyChangedCallback((s, e)=>
{
})));
}
public class ClassB : ClassA
{
public ClassB()
{
TestPropertyProperty.AddOwner(typeof(ClassB), new PropertyMetadata((s, e) =>
{
}));
}
}
public partial class MainWindow : Window
{
public ClassB TestClassB
{
get { return (ClassB)GetValue(TestClassBProperty); }
set { SetValue(TestClassBProperty, value); }
}
public static readonly DependencyProperty TestClassBProperty =
DependencyProperty.Register("TestClassB", typeof(ClassB), typeof(MainWindow), new PropertyMetadata(null));
public MainWindow()
{
InitializeComponent();
TestClassB = new ClassB();
TestClassB.TestProperty = "test";
}
}
}
-1
public class ClassA : DependencyObject
{
/// <summary>
///
/// </summary>
public string PropertyA
{
get { return (string)GetValue(PropertyAProperty); }
set { SetValue(PropertyAProperty, value); }
}
/// <summary>
/// Identifies the <see cref="PropertyA"/> dependency property.
/// </summary>
public static readonly DependencyProperty PropertyAProperty =
DependencyProperty.Register("PropertyA", typeof(string), typeof(ClassA), new PropertyMetadata("A"));
}
public class ClassB : ClassA
{
/// <summary>
///
/// </summary>
public string PropertyB
{
get { return (string)GetValue(PropertyBProperty); }
set { SetValue(PropertyBProperty, value); }
}
/// <summary>
/// Identifies the <see cref="PropertyB"/> dependency property.
/// </summary>
public static readonly DependencyProperty PropertyBProperty =
DependencyProperty.Register("PropertyB", typeof(string), typeof(ClassA), new PropertyMetadata("B"));
public ClassB()
{
ClassA.PropertyAProperty.OverrideMetadata(typeof(ClassB), new PropertyMetadata(AValuePropertyChanged));
}
private static void AValuePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show(e.NewValue.ToString());
}
}
public partial class MainWindow4 : Window
{
/// <summary>
///
/// </summary>
public MainWindow4()
{
InitializeComponent();
this.Reference = new ClassB();
}
private ClassB Reference { get; set; }
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Reference.PropertyA = "hello";
}
}
相關問題
- 1. 對WPF控件派生類的非WPF實例回調。變化的DependencyProperty
- 2. 基於DependencyProperty綁定的WPF樣式DataTrigger
- 3. WPF - DependencyProperty忽略setter的變化帶來的副作用
- 4. WPF中的DependencyProperty和UserControl
- 5. WPF DependencyProperty用法
- 6. WPF樣式的引用類型DependencyProperty
- 7. WPF將DependencyProperty綁定到另一個DependencyProperty
- 8. wpf DependencyProperty短期不接受默認值
- 9. 確定是否繼承WPF DependencyProperty值
- 10. WPF DependencyProperty拋出InvalidOperationException
- 11. 的DependencyProperty變化不會觸發安排
- 12. 如何從UI自動化框架中獲取WPF UserControl DependencyProperty的值?
- 13. WPF的DependencyProperty鏈通知
- 14. 基於變化的內聯變量的值添加類?
- 15. 通過wpf中的DependencyProperty發送UIElements
- 16. 在UserControl中公開的WPF DependencyProperty?
- 17. WPF MVVM TextBlock綁定到類中的字符串DependencyProperty
- 18. WPF從ViewModel綁定到DependencyProperty
- 19. DependencyProperty和WPF設計器
- 20. WPF DependencyProperty和數據綁定
- 21. 無法綁定WPF DependencyProperty
- 22. WPF - 更新DependencyProperty元數據
- 23. 如何在派生類的c#中設置基類的值wpf
- 24. DependencyProperty默認值
- 25. 在wpf中設置DependencyProperty屬性 - 類型轉換
- 26. 類變量將基類後初始化
- 27. 變化WPF
- 28. 在WPF中使用基類
- 29. WPF使用DependencyProperty改變UserControl內的標籤背景
- 30. 基於LINQ標準的價值變化