Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a Thickness using resources

In a windows UWP app project, I am trying to define a Thickness by assigning to its Left, Top, Right and Bottom properties:

<Setter Property="Margin">
    <Setter.Value>
        <Thickness Left="{StaticResource SomeDouble}"
                   Top="0"
                   Right="0"
                   Bottom="0" />
    </Setter.Value>
</Setter>

This answer seems to suggest that this is possible in WPF, however, in my UWP project (as well as in a WinRT app), I get the following error:

XAML Thickness type cannot be constructed. 
In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic 
or a struct, and must have a public default constructor.

Is there a way to define the Thickness using resources?

like image 685
Henrik Ilgen Avatar asked Feb 02 '16 09:02

Henrik Ilgen


1 Answers

You still can. Only to change System:Double to x:Double. e.g.

<x:Double x:Key="SomeDouble">12</x:Double>

Update

Interesting, with the above xaml the designer shows up fine but it doesn't compile... Yes it gives the exact same error like you showed.

So I guess you have to define the whole Thickness in xaml.

<Thickness x:Key="Thickness1">12,0,0,0</Thickness>

<Setter Property="Margin" Value="{StaticResource Thickness1}" />

I ran the project this time and yeah it worked. :)

like image 118
Justin XL Avatar answered Oct 04 '22 12:10

Justin XL