在WPF应用程序中,我已经定义了PropertyAnalysisTemplate附加属性,我希望它能够用于几种类型的控件。
我这样定义:
componentWillMount() {
this.setState({ myArray: this.props.yourReduxStoredProp });
}
_sortList() => {
// retrieve your array from state
let myArray = this.state.myArray;
// sort the array locally
myArray.sort(function(a, b) {
return b.sortKey- a.sortKey;
});
// update the state and, because of the change it state, the component re-renders
this.setState({ myArray: myArray });
}
一切正常,但在设计师视图中我收到错误:public static readonly DependencyProperty PropertyAnalysisTemplateProperty = DependencyProperty.RegisterAttached(
"PropertyAnalysisTemplate", typeof(DataTemplate), typeof(FrameworkElement), new PropertyMetadata(
default(DataTemplate))
);
public static DataTemplate GetPropertyAnalysisTemplate(DependencyObject obj)
{
return (DataTemplate) obj.GetValue(PropertyAnalysisTemplateProperty);
}
public static void SetPropertyAnalysisTemplate(DependencyObject obj, DataTemplate value)
{
obj.SetValue(PropertyAnalysisTemplateProperty, value);
}
我想我需要为我使用它的特定元素注册它(在这种特定情况下为TextBox),并使用AddOwners添加其他所有者。
但说实话,这对我来说看起来很糟糕。是不是有更多以对象为导向的方法将附加属性注册到基类及其所有后代?