0

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?

ASh
  • 34,632
  • 9
  • 60
  • 82
user34299
  • 377
  • 2
  • 11
  • 1
    the problem is that you are trying to bind multiple values with a single string, you need to either create a thickness definition that sets all border sizes or user a multi binding – MikeT Aug 23 '17 at 14:18
  • Possible duplicate of [Binding only part of the margin property of WPF control](https://stackoverflow.com/questions/6249518/binding-only-part-of-the-margin-property-of-wpf-control) – MikeT Aug 23 '17 at 14:22

2 Answers2

4

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

ASh
  • 34,632
  • 9
  • 60
  • 82
0

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.

luxun
  • 457
  • 5
  • 14
  • luxun, Actually I want to use `thick` and `thin` for different sides of the many TextBoxes, which is why I specified them separately. – user34299 Aug 23 '17 at 14:20
  • Ah I missed that. I think @ASh 's answer will give you what you want. – luxun Aug 23 '17 at 14:28