tags:

views:

45

answers:

1

I've been trying to specify a dimension in pixels as a resource, so I can then use it in multiple places in my UI. e.g.

...
<ResourceDictionary 
  xmlns:System="clr-namespace:System;assembly=mscorlib">
  <System:Double x:Key="a-width">140px</System:Double>
</ResourceDictionary 
...
<TextBlock 
  Width="{StaticResource a-width}" 
  Text="this TextBlock is a-width wide"/>
...

However, this doesn't work. Although Width is of type 'double', you can't specify the value of a double using the 'px' suffix, as this format is supported by the LengthConverter, which is the TypeConverter for the FrameworkElement.Width property.

Is there a way to specify a width in 'Npx' format as a resource?

+2  A: 

px (DIPs) is the default, so you can just specify the double value with the 'px' suffix:

<ResourceDictionary 
  xmlns:System="clr-namespace:System;assembly=mscorlib">
  <System:Double x:Key="a-width">140</System:Double>
</ResourceDictionary 
...
<TextBlock 
  Width="{StaticResource a-width}" 
  Text="this TextBlock is a-width wide"/>

HTH, Kent

Kent Boogaart