tags:

views:

498

answers:

3

I have the following two buttons in XAML:

<Button Content="Previous"
        Margin="10,0,0,10"/>
<Button Content="Next"
        Margin="0,0,10,10"/>

How can I define "10" to be a variable so I can change it in one place, something like this:

PSEUDO CODE:

<variable x:key="theMargin"/>
<Button Content="Previous"
        Margin="{Variable theMargin},0,0,{Variable theMargin}"/>
<Button Content="Next"
        Margin="0,0,{Variable theMargin},{Variable theMargin}"/>
A: 

Declare it as a (possibl static readonly or const) field in your codebehind file.

Rytmis
A: 

Why don't you try adding the value as a StaticResource?

Resources.Add("theMargin", 10);

Then you can get that value like this:

<Button Content="Previous"
        Margin="{StaticResource theMargin},0,0,{StaticResource theMargin}"/>
<Button Content="Next"
        Margin="0,0,{StaticResource theMargin},{StaticResource theMargin}"/>
Andrew Hare
I added Resources.Add("theMargin", 50); after InitializeComponent in my code behind, but XAML couldn't access it.
Edward Tanguay
+6  A: 

Try this:

add to the head of the xamlfile

xmlns:System="clr-namespace:System;assembly=mscorlib"

Then Add to the resouce section:

<System:Double x:Key="theMargin">2.35</System:Double>

Last, use a thickness on the margin:

<Button Content="Next">
   <Button.Margin>
      <Thickness Top="{StaticResource theMargin}" Left="0" Right="0"
                  Bottom ="{StaticResource theMargin}" />
   </Button.Margin>
</Button>

A lot of system types can be defined this way: int, char, string, DateTime, etc

Note: You're right... Had to do some better testing.. changed to code so that it should work

Sorskoot
hmmm, I add <System:Double x:Key="theMargin">2.35</System:Double> in Window.Resources and got "System:Double was not found", added "System" as a reference but didn't help, what am I missing?
Edward Tanguay
Double doesn't come up in the dropdown, only e.g. "RegistryHive" and three others, I've references System.Core, what else do I have to reference?
Edward Tanguay
I found my issue: I was adding this xmlns:System="clr-namespace:Microsoft.Win32;assembly=mscorlib" and should have been adding this: xmlns:System="clr-namespace:System;assembly=mscorlib"
Edward Tanguay