积极答复者
附加属性或依赖属性在初始化的时候,怎么处理所属的对象?

问题
答案
-
Hello,
根据你的描述, 假如你新建一个依赖属性或者附加属性, 你是想在PropertyChangedCallBack事件中得到属性所属的对象, 然后进行一系列操作是吗?
如果是的话, 我做了一个简单的例子, 你可以看一下:
如下面代码所示, 我在window9中新建一个myvalue的依赖属性, 这个依赖属性所属对象时window9。
public partial class Window9 : Window { public int myvalue { get { return (int)GetValue(myvalueProperty); } set { SetValue(myvalueProperty, value); } } // Using a DependencyProperty as the backing store for myvalue. This enables animation, styling, binding, etc... public static readonly DependencyProperty myvalueProperty = DependencyProperty.Register("myvalue", typeof(int), typeof(Window9), new PropertyMetadata(0,new PropertyChangedCallback(Propertychangedcallback))); private static void Propertychangedcallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { Window9 window = d as Window9; MessageBox.Show(String.Format("PropertyChanged - PropertyName:{0} New Value:{1} Old value:{2}", e.Property.Name, e.NewValue, e.OldValue)); } public Window9() { InitializeComponent(); myvalue = 10; this.DataContext = this; } }
Propertychangedcallback事件中的dependencyobject就是指所属的对象, window9.
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
全部回复
-
Hello,
根据你的描述, 假如你新建一个依赖属性或者附加属性, 你是想在PropertyChangedCallBack事件中得到属性所属的对象, 然后进行一系列操作是吗?
如果是的话, 我做了一个简单的例子, 你可以看一下:
如下面代码所示, 我在window9中新建一个myvalue的依赖属性, 这个依赖属性所属对象时window9。
public partial class Window9 : Window { public int myvalue { get { return (int)GetValue(myvalueProperty); } set { SetValue(myvalueProperty, value); } } // Using a DependencyProperty as the backing store for myvalue. This enables animation, styling, binding, etc... public static readonly DependencyProperty myvalueProperty = DependencyProperty.Register("myvalue", typeof(int), typeof(Window9), new PropertyMetadata(0,new PropertyChangedCallback(Propertychangedcallback))); private static void Propertychangedcallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { Window9 window = d as Window9; MessageBox.Show(String.Format("PropertyChanged - PropertyName:{0} New Value:{1} Old value:{2}", e.Property.Name, e.NewValue, e.OldValue)); } public Window9() { InitializeComponent(); myvalue = 10; this.DataContext = this; } }
Propertychangedcallback事件中的dependencyobject就是指所属的对象, window9.
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.