user34299
user34299

Reputation: 397

How can I assign property value in XAML using multiple StaticResources

In MainWindow.xaml I am trying to use two StaticResource elements to define the thickness of the border of a TextBox in a Grid. I cannot seem to use the StaticResource more than once in a BorderThickness specification.

The code snippets which work are,

xmlns:syw="clr-namespace:System.Windows;assembly=PresentationFramework"
<Window.Resources>
    <syw:Thickness x:Key="thick">5.0</syw:Thickness>
    <syw:Thickness x:Key="thin">1.0</syw:Thickness>
</Window.Resources>

After the number of rows and columns are defined, and still within the <Grid> section,

<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., thick and thin. 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?

Upvotes: 0

Views: 600

Answers (2)

ASh
ASh

Reputation: 35720

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:

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

</TextBox.BorderThickness>

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

Upvotes: 4

luxun
luxun

Reputation: 517

I think what you want to do here is define a thickness with your required parameters like so:

<Window.Resources>
    <syw:Thickness x:Key="borderThickness">5.0, 5.0, 1.0, 1.0</syw:Thickness>
</Window.Resources>

You can use this as follows:

<TextBox BorderThickness="{StaticResource borderThickness}"/>

This should give you the behaviour you're looking for.

Upvotes: 0

Related Questions