如何指定XAML值(例如转换器参数)区域性 [英] How to specify XAML value (e.g. converter parameter) culture

查看:63
本文介绍了如何指定XAML值(例如转换器参数)区域性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在XAML中使用静态双精度值时,如何指定以哪种格式提供它们?

When we use static double values in XAML, how can we specify in which format they are provided?

示例:

<Rectangle>
  <Rectangle.Opacity>
    <Binding Path="IsDimmed" Converter="{StaticResource boolToDoubleConverter}" ConverterParameter="0.8"/>
  </Rectangle.Opacity>
</Rectangle>

使用转换方法

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
   return double.Parse((string)parameter, culture);
}

问题是在机器上,小数用<$ c分隔$ c>,,转换方法分别失败或返回错误的值。

The problem is that on a machine, where decimals are separated by ,, the conversion method fails or returns a wrong value, respectively.

我不想在<中设置InvariantCulture code> Parse()方法,因为转换器可能在某些地方用于解析用户输入,这就是为什么 culture 参数的原因

I do not want to set the InvariantCulture in the Parse() method, because the converter might in some place be used to parse user input, which is why the culture parameter should still be applied.

如果我使用< sys:Double x:Key = dimValue> 0.8将值指定为资源< / sys:Double> ,转换器参数变为double类型,但在解析之前将其转换为字符串。

If I specify the value as a resource with <sys:Double x:Key="dimValue">0.8</sys:Double>, the converter parameter becomes a double type but before being parsed is cast to a string.

所以我会想要以某种方式指定,该值以en-GB格式表示,例如 ConverterParameter = 0.8 ConverterParameterCulture = en-GB />

So I would like to specify somehow, that the value is indicated in en-GB format like ConverterParameter="0.8" ConverterParameterCulture="en-GB"/>.

是否可以通过某种方式而不需要创建浮肿的MultiValueConverter?

Is this possible somehow without the need to create a puffy MultiValueConverter?

推荐答案

使用 ConverterCulture 属性的绑定

<Binding Path="IsDimmed" Converter="{StaticResource boolToDoubleConverter}" 
         ConverterParameter="0.8" ConverterCulture="en-GB"/>

这篇关于如何指定XAML值(例如转换器参数)区域性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆