财产已经被'FrameworkElement的“注册 [英] property was already registered by 'FrameworkElement'

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

问题描述

我写两个依赖属性和我不断收到[属性]已经被'FrameworkElement的注册错误VS11的设计窗口。下面是我的代码



 公共静态只读的DependencyProperty IsEditingNumberProperty = 
DependencyProperty.Register(IsEditingNumbers片段的typeof (布尔)的typeof(FrameworkElement的),
新FrameworkPropertyMetadata(真,FrameworkPropertyMetadataOptions.AffectsRender));



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



其实,它正确地呈现,但只有当我第一次打开它。紧随后的第一个渲染它会给我一个例外。在运行时,它似乎很好地工作。



我是不是做错了什么,是有摆脱这恼人的错误的方式?



---- -----编辑



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

 公共部分类EditableTextBox:用户控件
{
#地区的依赖属性
公共静态只读的DependencyProperty IsEditingNumberProperty =
DependencyProperty.Register(IsEditingNumber的typeof(布尔)的typeof(FrameworkElement的),
新FrameworkPropertyMetadata(真,FrameworkPropertyMetadataOptions.AffectsRender));

公共静态只读的DependencyProperty TextProperty =
DependencyProperty.Register(文本的typeof(串)的typeof(FrameworkElement的),
新FrameworkPropertyMetadata(0,FrameworkPropertyMetadataOptions.AffectsRender )
{
CoerceValueCallback =新CoerceValueCallback((发件人值)=>
{
返回expressionRestaraint.Match((字符串)值).value的;
} )
});
#endregion

公共静态正则表达式expressionRestaraint =新的正则表达式([ - A-ZA-Z0-9 + * \\(\\)\\ [。 \\] \\ {} \\] *);

公共字符串文本
{
获得{(串)的GetValue(TextProperty); }

{
的SetValue(TextProperty,值);
tbxValue.Text =(字符串)的GetValue(TextProperty);
}
}

公共BOOL IsEditingNumber
{
得到
{
返回(布尔)的GetValue(IsEditingNumberProperty);
}

{
布尔旧=(布尔)的GetValue(IsEditingNumberProperty);
如果(旧=价值!)
{
如果
stopEditing方法()(值!);
,否则
startEditing();

的SetValue(IsEditingNumberProperty,值);
}
}
}。 。 。

使用在主类:

 <窗​​口x:类=VisualMathExpression.MainWindow
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns :X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
的xmlns:编辑=CLR的命名空间:VisualMathExpression.EditableTextBox
的xmlns:所有=CLR的命名空间: VisualMathExpression
标题=主窗口HEIGHT =350WIDTH =525>
<&StackPanel的GT;
<编辑:EditableTextBox的Horizo​​ntalAlignment =中心VerticalAlignment =中心
文本=AFIsEditingNumber =真/> 。 。 。



---编辑---
包装固定的(问题导致XAML属性不当更改所有权属于申报类)

 公共部分类EditableTextBox:用户控件
{
#区域依赖属性
公共静态只读的DependencyProperty IsEditingNumberProperty =
DependencyProperty.Register(IsEditingNumber的typeof(布尔)的typeof(EditableTextBox),
新FrameworkPropertyMetadata(真,FrameworkPropertyMetadataOptions.AffectsRender)
{
PropertyChangedCallback =新PropertyChangedCallback((发件人,ARG)=>
{
EditableTextBox ED =发件人为EditableTextBox;
如果((布尔)arg.NewValue)$! b $ b ed.stopEditing();
,否则
ed.startEditing();
}),
});

公共静态只读的DependencyProperty TextProperty =
DependencyProperty.Register(文本的typeof(串)的typeof(EditableTextBox),
新FrameworkPropertyMetadata(0,FrameworkPropertyMetadataOptions.AffectsRender )
{
PropertyChangedCallback =新PropertyChangedCallback((发件人,ARG)=>
{
EditableTextBox ED =发件人为EditableTextBox;
ed.tbxValue.Text = ARG .NewValue作为字符串;
}),
CoerceValueCallback =新CoerceValueCallback((发件人值)=>
{
返回expressionRestaraint.Match((字符串)值).value的;
})
});
#endregion

公共静态正则表达式expressionRestaraint =新的正则表达式([ - A-ZA-Z0-9 + * \\(\\)\\ [。 \\] \\ {} \\] *);

公共字符串文本
{
{返回(串)的GetValue(TextProperty); }
集合{的SetValue(TextProperty,值); }
}

公共BOOL IsEditingNumber
{
{返回(布尔)的GetValue(IsEditingNumberProperty); }
集合{的SetValue(IsEditingNumberProperty,值); }
}


解决方案

第三个参数 ownerType href=\"http://msdn.microsoft.com/en-us/library/ms597502.aspx\"> DependencyProperty.Register 方法必须是。声明的属性的类



如果你的类是 MyClass的的声明必须是这样的:

 公共MyClass类:DependencyObject的
{
公共静态只读的DependencyProperty IsEditingNumberProperty =
DependencyProperty.Register (
IsEditingNumber的typeof(布尔)的typeof(MyClass的),...);

// CLR包装
公共BOOL IsEditingNumber
{
{返回(布尔)的GetValue(IsEditingNumberProperty); }
集合{的SetValue(IsEditingNumberProperty,值); }
}
}


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

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

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

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)

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

解决方案

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

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天全站免登陆