Override text property of text field

How to override text property in text field in WPF ?

I want this code in WPF:

public override string Text { get { return Text.Replace(",", ""); } set { Text = value; } } 
+4
source share
4 answers

If you bind data to the TextBox.Text property, then another possible approach is to divert the logic from the control itself and place it in the converter. Your converter will look something like this ...

 public class CommaReplaceConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value.ToString().Replace(",", ""); } } 

And the data is tied to something like this ...

 <TextBox Text="{Binding XXX, Converter={StaticResource CRC}" /> 

... where a static resource is defined as ...

 <Window.Resources> <CommaReplaceConverter x:Key="CRC"/> </Windows.Resources> 
+3
source

Text is a dependency property. If you got the class from a TextBox, you need to override the metadata and provide your validation callbacks

Cm

+2
source

Although the correct answers are correct, there is a much simpler approach: since the Text TextBox property is bound to the property of the base class (usually in the Viewmodel), just take care of the replacement in the base class.

0
source

Are you just missing a text argument?

 public override string Text { get { return Text.Replace(",", Text); } set { Text = value; } } 
-1
source

All Articles