在WPF中获取"<Property Name>"已在"<Control Name>"中注册错误

Rya*_*yan 15 c# wpf xaml dependency-properties

我在WPF中有一个用户控件,它具有绑定到依赖属性.当我尝试编译应用程序时,我得到一个"属性名称"已经被"ControlName"错误注册,并且设计者显示"无法创建"用户控件"错误的实例.

这是我的简单控件的样子:

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>
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

它托管的主窗口:

<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>
Run Code Online (Sandbox Code Playgroud)

这是设计师的样子:

设计师错误的屏幕截图

以下是XAML设计师的样子: XAML

Rya*_*yan 23

发生的事情是Dependency Property在同一名称和所有者下多次注册.依赖属性旨在拥有单个所有者,并且应该静态实例化.如果您没有静态实例化它们,则会尝试为每个控件实例注册它们.

使您的DependencyProperty声明静态.改变它:

 public DependencyProperty SomeStringValueProperty =
                             DependencyProperty.Register("SomeStringValue", 
                                                         typeof(string), 
                                                         typeof(ExampleUserControl));
Run Code Online (Sandbox Code Playgroud)

至:

public static DependencyProperty SomeStringValueProperty =
                             DependencyProperty.Register("SomeStringValue", 
                                                         typeof(string), 
                                                         typeof(ExampleUserControl));
Run Code Online (Sandbox Code Playgroud)


Dec*_*lor 8

我的错误消息是通过向基类注册依赖项属性引起的

即这个

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

而不是这个

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

我的WorkerControl类派生自Control