LoginSignup
2
2

More than 5 years have passed since last update.

ChangePropertyAction で小数を設定する時の注意

Last updated at Posted at 2014-05-31

Blend SDK の ChangePropertyAction

Blend SDK に含まれる ChangePropertyAction を使うと XAML上で簡単に要素の値を変更する事ができます。
以下は例としてボタンがクリックされた際にプログレスバーの値を1秒間かけて 0.5 に変更します。
(Storyboard 無しでアニメーションできるのも便利!)

<Button>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <ei:ChangePropertyAction TargetName="progressBar" 
                                     PropertyName="Value"
                                     Value="0.5"
                                     Duration="0:0:1" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

Culture 変更による問題

アプリ内の言語設定を以下のようにドイツ語に変更します。

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var de = new CultureInfo("de");
    Thread.CurrentThread.CurrentCulture = de; // 今回問題になるのはこっちの設定
    Thread.CurrentThread.CurrentUICulture = de;
}

上記の設定の後、ボタンをクリックすると以下の例外が発生してしまいます。

System.Exception: 0.5 is not a valid value for Double.
 ---> System.FormatException: Input string was not in a correct format.
   at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.Double.Parse(String s, NumberStyles style, IFormatProvider provider)
   at System.ComponentModel.DoubleConverter.FromString(String value, NumberFormatInfo formatInfo)
... 続く

理由は以下のリンクを見るとわかります。
http://ja.wikipedia.org/wiki/%E5%B0%8F%E6%95%B0%E7%82%B9

なんと、ドイツでは小数点はカンマで表します。
(他にイタリアやフランスなど)
なので、 Value="0.5" ではなく Value="0,5" と書かないと正しくパースされないようです。
しかも、トリガーの Invoke() が実行されるタイミングでパースが走るので、頻繁にカルチャが切り替わるシーンでは問題になります。

パッと思いついた対策は以下のように StaticResouce を使用する方法です。
ちょっと面倒ですね。。。

<Button>
    <Button.Resources>
        <sys:Double x:Key="progressBarValue">0.5</sys:Double>
    </Button.Resources>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <ei:ChangePropertyAction TargetName="progressBar"
                                     PropertyName="Value"
                                     Value="{StaticResource progressBarValue}"
                                     Duration="0:0:1" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2