【问题标题】:How to: Override Metadata for a Dependency Property如何:覆盖依赖属性的元数据
【发布时间】:2011-10-01 05:32:49
【问题描述】:

如何覆盖默认依赖属性元数据。 例如;文本框的文本属性。 我用这个代码

           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(",","");           
    }

但是这在两个运行中(get,set)

没有人可以帮助我!!!:(((

【问题讨论】:

标签: wpf


【解决方案1】:

这是一个派生自 TextBox 的类的示例,该类覆盖了 Text 属性的元数据:

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

请注意,覆盖位于static 构造函数中,而不是普通构造函数中。

【讨论】:

  • 谢谢,我想要 wpf 中的代码。 [get {return text.replace();} se{text=value;}]
  • 这是 WPF 代码。这就是进行自定义控件的方式(即:在派生自 UIElement 的类的静态构造函数中覆盖元数据)。
  • 获取Text属性时如何更改值?!
猜你喜欢
  • 2014-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-26
  • 2014-12-09
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多