【问题标题】:Create custom textblock创建自定义文本块
【发布时间】:2015-04-17 13:54:33
【问题描述】:

我想要一个 WPF 控件,它可以自动将其文本转换为大写。我不想在 WPF 的文本块中添加任何其他功能。

所以我想我可以创建一个这样的类:

public class UpperTextBlock : TextBlock
    {
        static UpperTextBlock()
        {

        }
        public UpperTextBlock()
         {

         }
    }

我只是想在“textchanged”上添加一个事件,一旦文本发生变化,只需将其设为大写,但我没有找到“textchanged”的等价物。我该怎么办?

谢谢!

第一次回答后编辑

我想在我的所有模板中使用我的自定义控件,而不仅仅是针对特定的文本块,这就是为什么转换器或类似第一个答案的东西对我来说不够通用。

【问题讨论】:

标签: c# wpf


【解决方案1】:

我能想到的最简单快捷的方法是从TextBlock 派生并强制TextBlock.TextProperty 值。为此,您需要覆盖属性元数据并指定强制回调。这是一个例子:

public class UpperTextBlock : TextBlock
{
    static UpperTextBlock()
    {
        TextBlock.TextProperty.OverrideMetadata(
            typeof(UpperTextBlock),
            new FrameworkPropertyMetadata(
                default(PropertyChangedCallback),
                (CoerceValueCallback)CoerceTextProperty));
    }

    private static object CoerceTextProperty(DependencyObject d, object baseValue)
    {
        if (baseValue is string)
            return ((string)baseValue).ToUpper();
        else
            return baseValue;
    }
}

【讨论】:

    【解决方案2】:

    如果您遵循 MVVM 模式,则可以在视图模型中执行此操作

    private string _textblock;
    public string TextBlock
    {
        get { return _textblock; }
        set { 
                _textblock = value.ToUpperInvariant(); 
                NotifyPropertyChanged("TextBlock"); 
            }
    }
    

    【讨论】:

    • 我想在我的所有模板中使用我的自定义控件,而不仅仅是针对特定的文本块,这就是为什么转换器或类似您的答案对我来说不够通用的原因。
    【解决方案3】:

    您可以使用Value Converter,它们是可重复使用的,并且是“WPF 方式”的处事方式。

    查看此link 了解更多信息。

    这是一个示例代码:

    public class ToLowerValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var str = value as string;
            return string.IsNullOrEmpty(str) ? string.Empty : str.ToUpper();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
    

    【讨论】:

      【解决方案4】:

      您可以创建另一个名为 uppertext 的依赖属性,并在依赖属性更改的属性上将文本块的文本设置为大写。参考下面的代码。

      class UpperTextBlock : TextBlock
      {
          public string UpperText
          {
              get { return (string)GetValue(UpperTextProperty); }
              set { SetValue(UpperTextProperty, value); }
          }
      
          // Using a DependencyProperty as the backing store for UpperText.  This enables animation, styling, binding, etc...
          public static readonly DependencyProperty UpperTextProperty =
              DependencyProperty.Register("UpperText", typeof(string), typeof(UpperTextBlock), new PropertyMetadata(string.Empty, OnCurrentReadingChanged));
      
          private static void OnCurrentReadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              UpperTextBlock txt = d as UpperTextBlock;
              txt.Text = txt.UpperText.ToUpper();
          }
      }
       <local:UpperTextBlock UpperText="test"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-12
        • 2014-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多