6

how to override default dependency property metadata. for example ;Text property for textbox. i use this code

           class UCTextBox : TextBox
       {
           public UCTextBox()
        {
       var defaultMetadata = TextBox.TextProperty.GetMetadata(typeof(TextBox));

       TextBox.TextProperty.OverrideMetadata(typeof(UCTextBox),
     new          FrameworkPropertyMetadata(string.Empty,
        FrameworkPropertyMetadataOptions.Journal | 
     FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
        defaultMetadata.PropertyChangedCallback,
        new CoerceValueCallback(CoerceText)
        )); 
}

    private static object CoerceText(DependencyObject d, object value)
     {
     return   value.ToString().Replace(",","");           
    }

but this In both runs(get,set)

No one can help me!!!:(((

ar.gorgin
  • 4,765
  • 12
  • 61
  • 100
  • 1
    possible duplicate of [How can I change the default value of an inherited dependency property?](http://stackoverflow.com/questions/5653364/how-can-i-change-the-default-value-of-an-inherited-dependency-property) – Jeff Mercado Oct 01 '11 at 06:02

1 Answers1

23

Here is an example of a class derived from TextBox overriding the metadata for the Text property:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

public class MyTextBox : TextBox
{
    static MyTextBox()
    {
        TextBox.TextProperty.OverrideMetadata(typeof(MyTextBox),
            new FrameworkPropertyMetadata(string.Empty,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault |
                FrameworkPropertyMetadataOptions.Journal,
                null, /* property changed callback */
                null, /* coerce value callback */
                true, /* is animation prohibited */
                UpdateSourceTrigger.LostFocus));
    }
}

Notice that the override is place in the static constructor, not the ordinary constructor.

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95