我正在尝试将一个PropertyChangedCallback添加到UIElement.RenderTransformOriginProperty.当我尝试覆盖PropertyMetadata时抛出异常.
我搜索过MSDN和谷歌,我能想到的就是这个.在该帖子的某些时候建议使用DependencyPropertyDescriptor.AddValueChanged,但这不会解决我的问题,因为这不是每个实例的回调.
我不明白这个例外意味着什么.有谁知道我做错了什么?
public class foo : FrameworkElement
{
private static void Origin_Changed( DependencyObject d,
DependencyPropertyChangedEventArgs e)
{ }
static foo()
{
PropertyMetadata OriginalMetaData =
UIElement.RenderTransformOriginProperty.GetMetadata(
typeof(FrameworkElement));
/*An exception is thrown when this line is executed:
"Cannot change property metadata after it has been associated with a property"*/
OriginalMetaData.PropertyChangedCallback +=
new PropertyChangedCallback(Origin_Changed);
UIElement.RenderTransformOriginProperty.OverrideMetadata(
typeof(foo), OriginalMetaData);
}
}
Run Code Online (Sandbox Code Playgroud) .net c# dependency-properties exception invalidoperationexception