XAML で浮動小数点値を直接指定し、それをいくつかの UI ピースのリソースとして使用すると非常に便利な状況に遭遇しました。いろいろと調べてみると、適切なアセンブリ (mscorlib) を XAML に含める方法について、かなりの量の情報が見つかりました。
残念ながら、これを行おうとすると例外が発生します。状況を再現する次の XAML を次に示します。
<Window x:Class="davidtestapp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<core:Double x:Key="MyDouble">120</core:Double>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{StaticResource MyDouble}" />
<ColumnDefinition Width="40" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0" Fill="Red" />
<Rectangle Grid.Column="1" Fill="Green" />
<Rectangle Grid.Column="2" Fill="Blue" />
</Grid>
</Window>
これをコンパイルして実行しようとすると、「'120' はプロパティ 'Width' の有効な値ではありません」という XamlParseException がスローされます。
しかし、「Width」プロパティはdouble なので、定義された StaticResource を使用して設定できないのはなぜですか? 誰もこれを行う方法を知っていますか?