Setting borderbrush to LinearGradientBrush in WPF

Peter S picture Peter S · Sep 16, 2011 · Viewed 13.2k times · Source

I'm new to WPF and still having some basic problems.

I have a control from devcomponents that defaults to a blue border. My textboxes etc. have a more grey colour. I want the devcomponents control to have the same border.

I look in the properties of a TextBox and see that BorderBrush is set to "System.Windows.Media.LinearGradientBrush" yet I can't put -

<WpfEditors:IntegerInput BorderBrush="System.Windows.Media.LinearGradientBrush"...

In fact, I can't put -

<TextBox BorderBrush="System.Windows.Media.LinearGradientBrush" ...

What magic am I missing?

Thanks.

Answer

fixagon picture fixagon · Sep 16, 2011

To the property BorderBrush you have to assign a Brush (as you could guess by its name).

One kind of Brush is a LinearGradientBrush (the thing which makes a gradient between colors) SolidColorBrush is another kind of Brush which could also get assigned.

As it looks as this kind of control you use has already assigned a LinearGradientBrush. Now you can assign a Brush of your choice and override the already set Brush.

Example for a LinearGradientBrush:

<TextBox>
  <TextBox.BorderBrush>
    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
      <GradientStop Color="Black" Offset="0.0" />
      <GradientStop Color="White" Offset="1" />
    </LinearGradientBrush>
  </TextBox.BorderBrush>
</TextBox>

If you want your border just in a a solid color you can also use a SolidColorBrush.

  <TextBox.BorderBrush>
    <SolidColorBrush Color="Red" />
  </TextBox.BorderBrush>

or just use the existing Converter Color --> SolidColorBrush

<TextBox BorderBrush="Red" Text="bla bla" />

EDIT:

And if you want that all your controls have the same Border you can add a Brush to the ResourceDictionary of a container object and reuse it for all the controls...

<!-- Add the Brush as resource to the surrounding window -->
<Window.Resources>
  <SolidColorBrush x:Key="controlBorderBrush" Color="Gray" />
</Window.Resources>

<!-- -->
<TextBlock BorderBrush="{StaticResource controlBorderBrush}" Text="huhuuu" />
<otherlib:SpecialTextBlockWithOverriddenProps BorderBrush="{StaticResource controlBorderBrush}" Text="hahaaaaaaa" />