Is开发者_高级运维 there anyway of getting the instance of an attached property from a property change callback sitting on the attached property type?
In other words, if you have:
public class MyAttachedPropertyClass
{
public static readonly DependencyProperty MyProperty = DependencyProperty.RegisterAttached(
"My", typeof(int), typeof(MyAttachedPropertyClass), new FrameworkPropertyMetadata(0, OnMyPropertyChanged));
private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//TODO: access instance of MyAttachedPropertyClass
}
}
I need to access the instance of MyAttachedPropertyClass so that I can set the value of another property sitting on the type.
Typically, in my experience at least, the type that attached properties are owned by is static. When not static, when it comes to setting an attached property, no instance of the owning type is instantiated. If that's what you really want, you're going to have to do a bit more work.
Essentially, you're going to need to instantiate the instance at some point yourself and then make it accessible in your OnMyPropertyChanged
, likely through some kind of static state.
I don't know exactly what you're trying to achieve, but as an alternative, you are able to set other attached properties on your DependencyObject
"d
" in OnMyPropertyChanged
. So, if there's some kind of state you want to keep around you could put it in annother attached property.
It occurs to me, at this point, that MyAttachedPropertyClass
may be a control of sorts that uses attached properties like the Grid
or DockPanel
. In that case, MyAttachedPropertyClass
may be a parent of the DependencyObject
"d
" and you can access it using the VisualTreeHelper class.
At this point, I'm just shooting in the dark with answers. If you have any more details about what you're trying to do, I may be able to help you further.
精彩评论