简体   繁体   中英

Create custom textblock

I'd like to have a WPF control that automatically transforms its text in uppercase. I don't want to add any other function to the WPF's textblock.

So I thought I could create a class like this :

public class UpperTextBlock : TextBlock
    {
        static UpperTextBlock()
        {

        }
        public UpperTextBlock()
         {

         }
    }

I just wanted to add an event on "textchanged" and once the text changes just put it in uppercase, but I didn't find the equivalent of "textchanged". How could I do ?

Thank you !

EDIT after first answers

I would like to use my custom control in all my templates and not only for a particular textblock, that's why a converter or something like the first answer isn't enough generic for me.

The easiest and quickest way I can think of is to derive from TextBlock and coerce TextBlock.TextProperty value. In order to do that you'll need to override the property metadata and specify a coerce callback. Here's an example:

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

If you follow the MVVM pattern, you can do this in your view model

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

You can use a Value Converter they are re-usable and the "WPF-way" of doing things.

Check this link for more info.

Here's a sample code:

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

You can create an another dependency property called uppertext and on the property changed of the dependency property you can make the Text of text block to upper case. Refer below code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM