I tried this out just to see what will happen and it did work but I have no idea why so. Can somebody explain me what is happening in the background of DependencyProperties
?
I have a class which declares a DependencyProperty
but then in another class I target that DependencyProperty
by using GetValue
and SetValue
.
Here is an example:
public class DependencyProperties : DependencyObject
{
public Size EstimatedSize
{
get { return (Size)GetValue(EstimatedSizeProperty); }
set { SetValue(EstimatedSizeProperty, value); }
}
public static readonly DependencyProperty EstimatedSizeProperty =
DependencyProperty.Register("EstimatedSize", typeof(Size), typeof(DependencyProperties), null);
}
public class MyControl: ContentControl
{
public Size CalculatedSize
{
get { return (Size)GetValue(DependencyProperties.EstimatedSizeProperty); }
set { SetValue(DependencyProperties.EstimatedSizeProperty, value); }
}
protected override OnApplyTemplate()
{
// This works but why? How is it possible to do this? What is happening under the hood?
this.CalculatedSize = new Size(123, 123);
}
}
Why is it possible to do this? What is happening in the background of this example? The MyControl class didnt register the DP but it can use it. Can somebody tell me what is happening under the hood?