在 WPF 中获取“<属性名称> 已被“<控件名称>”错误注册
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24214468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting "<Property Name> was already registered by "<Control Name>" error in WPF
提问by Ryan
I have a user control in WPF that has a binding to a Dependency Property. 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.
我在 WPF 中有一个用户控件,它绑定到依赖属性。当我尝试编译应用程序时,我收到一个“属性名称”已被“ControlName”错误注册,并且设计器显示“无法创建“用户控件”的实例错误。
Here is what my simple control looks like:
这是我的简单控件的样子:
ExampleUserControl.xaml:
示例用户控件.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 设计器如下所示:
回答by Ryan
What's happening is the Dependency Property is getting Registered multiple times under the same name and owner. 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. Change it from:
使您的 DependencyProperty 声明为静态。将其更改为:
public DependencyProperty SomeStringValueProperty =
DependencyProperty.Register("SomeStringValue",
typeof(string),
typeof(ExampleUserControl));
To:
到:
public static DependencyProperty SomeStringValueProperty =
DependencyProperty.Register("SomeStringValue",
typeof(string),
typeof(ExampleUserControl));
回答by Declan Taylor
My error message of this type was caused by registering a dependency property with a base class
我的这种类型的错误消息是由使用基类注册依赖属性引起的
i.e 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
回答by ΩmegaMan
I did a cut and paste between two controls for the dependency property and forgot to change the registered class away from the first control on the second. Hence duplication.
我在依赖属性的两个控件之间进行了剪切和粘贴,但忘记将注册的类从第二个控件中的第一个控件更改。因此重复。
public static readonly DependencyProperty CurrentBatchProperty =
DependencyProperty.Register(
"CurrentBatch",
typeof(Batch),
typeof({--- Must be same as encompassing class --}), // Error if not set properly.
new PropertyMetadata(null, OnCurrentBatchPropertyChanged));
For upon pasting the code, my original control was thenregistering it in two different controls and it got the squiggly line (on the placement page) and the error.
对于在粘贴代码,我原来的对照,然后对其进行注册在两个不同的控制,并得到了波浪线(放置页)和错误。

