简体   繁体   English

嵌套的 UserControl(s) 抛出“属性已被注册”

[英]Nested UserControl(s) throw "Property was already registered by"

I have this UserControl xaml:我有这个用户控件 xaml:

<Grid>
        <Separator x:Name="sep" VerticalAlignment="Center" Height="10" />
</Grid>

and its code behind where I define a DependencyProperty to edit the line color:及其背后的代码,我定义了一个 DependencyProperty 来编辑线条颜色:

public partial class SeparatorLineText : UserControl
    {
        public static DependencyProperty? ColorProperty;
        private PropertyMetadata meta = new PropertyMetadata(propertyChangedCallback: ColorChanged);
        public SeparatorLineText()
        {
            ColorProperty = DependencyProperty.Register("MyColor", 
                typeof(Brush), 
                typeof(SeparatorLineText), 
                meta);

            InitializeComponent();
        }

        public Brush MyColor
        {
            get { return (Brush)base.GetValue(ColorProperty); }
            set { base.SetValue(ColorProperty, value); }
        }

        private static void ColorChanged(object d, DependencyPropertyChangedEventArgs e)
        {
            ((SeparatorLineText)d).OnColorChanged(e);
        }

        protected virtual void OnColorChanged(DependencyPropertyChangedEventArgs e)
        {
            sep.Background = (Brush)e.NewValue;
        }
    }

Then I have this other UserControl which has SeparatorLineText inside:然后我有另一个 UserControl,里面有 SeparatorLineText:

<UserControl x:Class="MySubWindow" 
...
>

<Grid>
    <control:SeparatorLineText MyColor="Red"/>
</Grid>

Finally, in MainWindow.xaml I include MySubWindow which has SeparatorLineText inside:最后,在 MainWindow.xaml 中,我包含了 MySubWindow,其中包含 SeparatorLineText:

<control:MySubWindow x:Name="MyTab" VerticalAlignment="Top" Width="1280"/>

When i run the project it displays my custom separator correctly, but in the MainWindow's xaml designer it doesn't load correctly saying: " MyColor property was already registered by SeparatorLineText "当我运行该项目时,它会正确显示我的自定义分隔符,但在 MainWindow 的 xaml 设计器中,它无法正确加载并显示:“ MyColor 属性已由 SeparatorLineText 注册

I've already read the other topics about this but I didn't find a solution.我已经阅读了有关此的其他主题,但没有找到解决方案。

Dependency properties only need to be registered once.依赖属性只需要注册一次。 In your situation it is registered every time the constructor is called.在您的情况下,每次调用构造函数时都会注册它。 You can solve this with a static constructor:您可以使用 static 构造函数解决此问题:

public static DependencyProperty ColorProperty;

// You can also make this field static, since it is not bound to a specific instance.
private static PropertyMetadata meta = new PropertyMetadata(propertyChangedCallback: ColorChanged);

static SeparatorLineText()
{
    ColorProperty = DependencyProperty.Register("MyColor", 
        typeof(Brush), 
        typeof(SeparatorLineText), 
        meta);
}

public SeparatorLineText()
{
    InitializeComponent();
}

Or initialize it directly:或者直接初始化:

public static DependencyProperty ColorProperty = 
    DependencyProperty.Register("MyColor", 
        typeof(Brush), 
        typeof(SeparatorLineText), 
        meta);

// You can also make this field static, since it is not bound to a specific instance.
private static PropertyMetadata meta = new PropertyMetadata(propertyChangedCallback: ColorChanged);

public SeparatorLineText()
{
    InitializeComponent();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM