属性已由“FrameworkElement"注册 [英] property was already registered by 'FrameworkElement'

查看:26
本文介绍了属性已由“FrameworkElement"注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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.

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

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 -----

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

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

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);
            }
        }
    } . . .

在主类中使用:

<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" /> . . .

--- 编辑---Wrapper 修复(当所有权属于声明的类时导致 xaml 属性不改变的问题)

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

    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); }
    }

推荐答案

DependencyProperty.Register 方法必须是声明属性的类.

The third parameter ownerType of the DependencyProperty.Register method must be the class that declares the property.

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

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

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); }
    }
}

这篇关于属性已由“FrameworkElement"注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆