4

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.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
Peter S
  • 191
  • 1
  • 2
  • 10
  • You need to use an instance of a LinearGradientBrush.. you can't just supply the class name. – mdm20 Sep 16 '11 at 15:40

1 Answers1

13

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" />
jeuxjeux20
  • 391
  • 1
  • 6
  • 20
fixagon
  • 5,506
  • 22
  • 26
  • Thanks for that but I possibly need to ask a different question. I have have the XAML "", I notice that the BorderBrush is set to a linear gradient brush. How would I assign exactly the same brush to another control? You've shown me how I could add a brush but I can't see where TextBox is getting its default brush from so I can't see how to do the same for my other control. Thanks. – Peter S Sep 19 '11 at 08:21
  • Just realised I can do it in "code behind" with "this.TimeInput.BorderBrush = this.HostTextBox.BorderBrush;". Perhaps that's the answer? – Peter S Sep 19 '11 at 08:27
  • you can do that, but thats not really nice. you lose control where you define what. read my edit – fixagon Sep 19 '11 at 08:35
  • Hi - thanks for that. It's close but I still don't know what settings the TextBlock is using by default. So I can't declare a resource that just says "the same as TextBox's border brush" can I? Perhaps I can reverse engineer it by looking at the values in the debugger and then setting it in the xaml. – Peter S Sep 21 '11 at 14:37
  • I'm also looking for the default definition of the BorderBrush used by TextBox. I nabbed the control style for TextBox using Expression Blend, so I could make a custom control extending TextBox. However, it defined its BorderBrush as simply "TextBoxBorder", and I have not been able to get at that definition. – e-holder Jun 28 '12 at 19:06
  • 1
    Found it here: http://msdn.microsoft.com/en-us/library/cc645061%28v=VS.95%29.aspx – e-holder Jun 28 '12 at 20:01