我試過這個只是爲了看看會發生什麼,它確實工作,但我不知道爲什麼如此。有人可以解釋我在DependencyProperties
的背景中發生了什麼?DependencyProperty奇怪的行爲
我有一個聲明,但DependencyProperty
然後在另一個I類靶向DependencyProperty
使用GetValue
和SetValue
類。
下面是一個例子:
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);
}
}
爲什麼能夠做到這一點?這個例子的背景中發生了什麼? MyControl類沒有註冊DP,但可以使用它。有人能告訴我發動機罩下發生了什麼嗎?
可能重複:http://stackoverflow.com/questions/6843877/difference-between-attached-and-non-attached-dependency-properties-in-silverligh – Blachshma