Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define a variable in XAML?

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}"/> 
like image 293
Edward Tanguay Avatar asked May 18 '09 11:05

Edward Tanguay


People also ask

What is XAML code?

Extensible Application Markup Language (XAML /ˈzæməl/ ( listen)) is a declarative XML-based language that Microsoft developed for initializing structured values and objects.

What is generic XAML?

xaml. If the style is not found in the theme dictionary, it looks in Generic. xaml i.e for controls whose look doesn't depend on the theme. This only applies to any custom controls you have defined i.e. classes derived from Control, directly or indirectly.


1 Answers

Try this:

add to the head of the xamlfile

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

Then Add this to the resource section:

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

Lastly, 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

like image 53
Sorskoot Avatar answered Sep 29 '22 09:09

Sorskoot