user34299 user34299 - 6 months ago 49
C# Question

How can I assign property value in XAML using multiple StaticResources


I am trying to use two
elements to define the thickness of the border of a TextBox in a Grid. I cannot seem to use the
more than once in a

The code snippets which work are,


<syw:Thickness x:Key="thick">5.0</syw:Thickness>
<syw:Thickness x:Key="thin">1.0</syw:Thickness>

After the number of rows and columns are defined, and still within the

<TextBox Name="c00" Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="{StaticResource ResourceKey=thick}"/>
<TextBox Name="c01" Grid.Row="2" Grid.Column="2" BorderBrush="Black" BorderThickness="5.0, 5.0, 1.0, 1.0"/>

This code complies and displays two TextBoxes, the first with the same border thickness on all four sides of the TextBox, the second with one thickness for the left and top sides, and a second thickness for the right and bottom sides of the TextBox.

What I want to be able to do is use the StaticResource multiple times in place of the numbers in the second line immediately above since I have a lot of TextBoxes and want to be able to change the border thicknesses by changing a couple of numbers, viz.,
. However, when I try,

<TextBox Name="c00" Grid.Row="1" Grid.Column="1" BorderBrush="Black"
BorderThickness="{StaticResource ResourceKey=thick},{StaticResource ResourceKey=thick},{StaticResource ResourceKey=thin},{StaticResource ResourceKey=thin}"/>

the editor indicates that the comma is unexpected at that position and won't compile.

Am I just formatting this incorrectly?

Answer Source

declare additional resources

<sys:Double x:Key="dThick">5.0</sys:Double>
<sys:Double x:Key="dThin">1.0</sys:Double>

and set thickness using tag-syntax and double resource value for each side of border:

    <syw:Thickness Left="{StaticResource dThick}" Top="{StaticResource dThick}"
                   Right="{StaticResource dThin}" Bottom="{StaticResource dThin}"/>


BorderThickness="{StaticResource ResourceKey=thick}" is a markup extension and works fine.

BorderThickness="5.0, 5.0, 1.0, 1.0" works because there is associated type converter which convert a string with comma separated numbers to Thickness

BorderThickness="{StaticResource ResourceKey=thick},{StaticResource ResourceKey=thick},{StaticResource ResourceKey=thin},{StaticResource ResourceKey=thin}" - just not supported in xaml

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download