简体   繁体   English

得到“ <Property Name> 已经注册“ <Control Name> “WPF中的错误

[英]Getting “<Property Name> was already registered by ”<Control Name>" error in WPF

I have a user control in WPF that has a binding to a Dependency Property. 我在WPF中有一个用户控件,它具有绑定到依赖属性。 When I try to compile the app, I get a "Property Name" was already registered by "ControlName" error, and the designer shows a "Cannot create an instance of "User Control" error. 当我尝试编译应用程序时,我得到一个“属性名称”已经被“ControlName”错误注册,并且设计者显示“无法创建”用户控件“错误的实例。

Here is what my simple control looks like: 这是我的简单控件的样子:

ExampleUserControl.xaml: ExampleUserControl.xaml:

<UserControl x:Class="ExampleApp1.ExampleUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ExampleApp1"
             mc:Ignorable="d" >

    <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ExampleUserControl}}, Path=SomeStringValue}" />

</UserControl>

ExampleUserControl.xaml.cs: ExampleUserControl.xaml.cs:

public partial class ExampleUserControl : UserControl
{

    public DependencyProperty SomeStringValueProperty = DependencyProperty.Register("SomeStringValue", typeof(string), typeof(ExampleUserControl));
    public string SomeStringValue
    {
        get
        {
            return GetValue(SomeStringValueProperty) as string;
        }
        set
        {
            SetValue(SomeStringValueProperty, value);
        }
    }

    public ExampleUserControl()
    {
        InitializeComponent();
    }
}

The Main Window it's hosted in: 它托管的主窗口:

<Window x:Class="ExampleApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ExampleApp1"
        Title="MainWindow" Height="350" Width="525">

    <StackPanel>
        <local:ExampleUserControl SomeStringValue="Test 1" />
        <local:ExampleUserControl SomeStringValue="Test 2" />
    </StackPanel>

</Window>

Here is what the designer looks like: 这是设计师的样子:

设计师错误的屏幕截图

Here is what the XAML designer looks like: 以下是XAML设计师的样子: XAML

What's happening is the Dependency Property is getting Registered multiple times under the same name and owner. 发生的事情是Dependency Property在同一名称和所有者下多次注册。 Dependency Properties are intended to have a single owner, and should be statically instanced. 依赖属性旨在拥有单个所有者,并且应该静态实例化。 If you don't statically instance them, an attempt will be made to register them for each instance of the control. 如果您没有静态实例化它们,则会尝试为每个控件实例注册它们。

Make your DependencyProperty declaration static. 使您的DependencyProperty声明静态。 Change it from: 改变它:

 public DependencyProperty SomeStringValueProperty = DependencyProperty.Register("SomeStringValue", typeof(string), typeof(ExampleUserControl));

To: 至:

public static DependencyProperty SomeStringValueProperty = DependencyProperty.Register("SomeStringValue", typeof(string), typeof(ExampleUserControl));

My error message of this type was caused by registering a dependency property with a base class 我的错误消息是通过向基类注册依赖项属性引起的

ie this 即这个

public static readonly DependencyProperty WorkerStateProperty =
    DependencyProperty.Register("WorkerState", typeof(State), typeof(Control),
        new FrameworkPropertyMetadata(State.Stopped, new PropertyChangedCallback(OnWorkerStateChanged)));

instead of this 而不是这个

public static readonly DependencyProperty WorkerStateProperty =
    DependencyProperty.Register("WorkerState", typeof(State), typeof(WorkerControl),
        new FrameworkPropertyMetadata(State.Stopped, new PropertyChangedCallback(OnWorkerStateChanged)));

where my WorkerControl class derived from Control 我的WorkerControl类派生自Control

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

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