如何覆盖 WPF 中文本框中的文本属性?
我想要 WPF 中的这段代码:
public override string Text
{
get { return Text.Replace(",", ""); }
set { Text = value; }
}
最佳答案
如果您对 TextBox.Text 属性进行数据绑定(bind),那么另一种可能的方法是将逻辑从控件本身移开并将其放置在转换器中。您的转换器看起来像这样......
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(",", "");
}
}
还有像这样的数据绑定(bind)......
<TextBox Text="{Binding XXX, Converter={StaticResource CRC}" />
...其中静态资源定义为...
<Window.Resources>
<CommaReplaceConverter x:Key="CRC"/>
</Windows.Resources>
关于c# - 覆盖文本框的文本属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7592748/