Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

c# - property was already registered by '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));

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); }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...