wpf 属性已由“FrameworkElement”注册

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13131116/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:58:53  来源:igfitidea点击:

property was already registered by 'FrameworkElement'

c#wpfpropertiesframeworkselement

提问by rbtLong

I am writing two dependency properties and I keep getting the "[Property] was already registered by 'FrameworkElement'" error in the design window of VS11. Here is a snippet of my code

我正在编写两个依赖项属性,并且在 VS11 的设计窗口中不断收到“[Property] 已被 'FrameworkElement' 注册”错误。这是我的代码片段

        public static readonly DependencyProperty IsEditingNumberProperty =
        DependencyProperty.Register("IsEditingNumbers", typeof(bool), typeof(FrameworkElement),
        new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));

the problem seems to be the 3rd parameter (owner parameter typeof(FrameworkElement)). If I set the 3rd parameter to the class the contains two dependency properties, the error goes away, but I cannot use the properties directly from xaml. I would have to add ownership for each dependency property before I use it.

问题似乎是第三个参数(所有者参数 typeof(FrameworkElement))。如果我将第三个参数设置为包含两个依赖属性的类,错误就会消失,但我不能直接从 xaml 使用这些属性。在使用之前,我必须为每个依赖属性添加所有权。

Actually, It does render correctly, but only when I first open it. Immediately after the first render it will give me an exception. At runtime, it seems to work perfectly.

实际上,它确实呈现正确,但仅当我第一次打开它时。在第一次渲染之后,它会立即给我一个例外。在运行时,它似乎完美地工作。

Am I doing something wrong and is there a way to get rid of this annoying error?

我做错了什么,有没有办法摆脱这个烦人的错误?

---- Edit -----

- - 编辑 - - -

Here is my custom class (includes 2 of the Dependency Properties):

这是我的自定义类(包括 2 个依赖属性):

public partial class EditableTextBox : UserControl
{
    #region Dependency Properties
    public static readonly DependencyProperty IsEditingNumberProperty =
        DependencyProperty.Register("IsEditingNumber", typeof(bool), typeof(FrameworkElement),
        new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(FrameworkElement),
        new FrameworkPropertyMetadata("0", FrameworkPropertyMetadataOptions.AffectsRender) 
        { 
            CoerceValueCallback = new CoerceValueCallback((sender,value) =>
                {
                    return expressionRestaraint.Match((string)value).Value;
                })
        });
    #endregion

    public static Regex expressionRestaraint = new Regex("[-a-zA-z0-9+*.\(\)\[\]\{\}]*");

    public string Text
    {
        get { (string)GetValue(TextProperty); }
        set 
        { 
            SetValue(TextProperty, value);
            tbxValue.Text = (string)GetValue(TextProperty);
        }
    }

    public bool IsEditingNumber
    {
        get 
        { 
            return (bool)GetValue(IsEditingNumberProperty); 
        }
        set 
        {
            bool old = (bool)GetValue(IsEditingNumberProperty);
            if (old != value)
            {
                if (!value)
                    stopEditing();
                else
                    startEditing();

                SetValue(IsEditingNumberProperty, value);
            }
        }
    } . . .

Use in Main Class:

在主类中使用:

<Window x:Class="VisualMathExpression.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:edit="clr-namespace:VisualMathExpression.EditableTextBox"
    xmlns:all="clr-namespace:VisualMathExpression"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <edit:EditableTextBox HorizontalAlignment="Center" VerticalAlignment="Center"
                          Text="af" IsEditingNumber="True" /> . . .

--- Edit --- Wrapper fixed (problem that cause xaml property not to change when ownership belonged to the declared class)

--- 编辑 --- 包装器已修复(当所有权属于声明的类时导致 xaml 属性不更改的问题)

    public partial class EditableTextBox : UserControl
{
    #region Dependency Properties
    public static readonly DependencyProperty IsEditingNumberProperty =
        DependencyProperty.Register("IsEditingNumber", typeof(bool), typeof(EditableTextBox),
        new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender) 
        { 
            PropertyChangedCallback = new PropertyChangedCallback((sender, arg) =>
                {
                    EditableTextBox ed = sender as EditableTextBox;
                    if (!(bool)arg.NewValue)
                        ed.stopEditing();
                    else
                        ed.startEditing();
                }),
        });

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(EditableTextBox),
        new FrameworkPropertyMetadata("0", FrameworkPropertyMetadataOptions.AffectsRender) 
        { 
            PropertyChangedCallback = new PropertyChangedCallback((sender,arg) =>
                {
                    EditableTextBox ed = sender as EditableTextBox;
                    ed.tbxValue.Text = arg.NewValue as string;
                }),
            CoerceValueCallback = new CoerceValueCallback((sender,value) =>
                {
                    return expressionRestaraint.Match((string)value).Value;
                })
        });
    #endregion

    public static Regex expressionRestaraint = new Regex("[-a-zA-z0-9+*.\(\)\[\]\{\}]*");

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public bool IsEditingNumber
    {
        get { return (bool)GetValue(IsEditingNumberProperty); }
        set { SetValue(IsEditingNumberProperty, value); }
    }

回答by Clemens

The third parameter ownerTypeof the DependencyProperty.Registermethod must be the class that declares the property.

第三个参数ownerType中的DependencyProperty.Register方法必须声明的属性的类。

If your class is MyClassthe declaration would have to look like this:

如果您的类是MyClass声明,则必须如下所示:

public class MyClass : DependencyObject
{
    public static readonly DependencyProperty IsEditingNumberProperty =
        DependencyProperty.Register(
            "IsEditingNumber", typeof(bool), typeof(MyClass), ...);

    // CLR wrapper
    public bool IsEditingNumber
    {
        get { return (bool)GetValue(IsEditingNumberProperty); }
        set { SetValue(IsEditingNumberProperty, value); }
    }
}