Question

I'm trying to define a common Width resource in Common.xaml which will be shared by different controls, like following:

<GridLength x:Key="CommonWidth">20</GridLength>

I then use it in a Button style defined in ButtonStyle.xaml

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Width" Value="{DynamicResource CommonWidth}"/>
    ....
</Style>

Common.xaml is included before ButtonStyle.xaml in the ResourceDictionary definition in App.xaml. When I ran the application (on .Net3.5 SP1), I got the following exception:

'20' is not a valid value for property 'Width'.

Anyone has any ideas of what I have done wrong? Thanks in advance.

Was it helpful?

Solution

Note that Button.Width is not of type GridLength. In Grid.GridLength you can specify the value as fixed, auto or star. It is only used for size of rows, columns in Grid.

Button.Width is of type double. That is if you want to set it using a resource you need a resource like

<sys:Double x:Key="Height">200</sys:Double>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top