我知道它已经被问过很多次了,我看到了很多关于它的帖子,但它仍然不起作用(我检查了http://lambert.geek.nz/2009/03/stringformatconverter/)。
例如:数字打印“12 236,3”。到目前为止,我有一个转换器。我检查并返回值格式良好,我在文本框上调用我的转换器,即使在调试我的转换器时也调用了但打印的值是“经典的”并且没有考虑我的格式。转换器:
public class NumericFieldConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double val = System.Convert.ToDouble(value);
string valString = val.ToString();
string finalString = string.Empty;
var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";
finalString = val.ToString("#,0.00", nfi); // "1 234 897.11"
finalString = finalString.Replace(".", ",");//"1 234 897,11"
return finalString;
}
public object ConvertBack(object value, Type targetType, object
parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是我在 XAML 中的绑定:
<TextBox Text="{Binding SelectedAvenant.TermeFixe0, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NumericFieldConverter}, Mode=TwoWay}" IsReadOnly="True" Grid.Row="5" Grid.Column="1"/>
实际上,我需要将转换器应用于我所有的文本框(带有样式),但我稍后会这样做。
您必须在窗口/用户控件/...上创建转换器的实例。
<Window ...>
<Window.Resources>
<local:NumericFieldConverter x:Key="converter" />
</Window.Resources>
</Window>
并在您的 TextBox 中使用它
Converter={StaticResource converter},
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句