Setting borderbrush to LinearGradientBrush in WPF
Asked Answered
K

1

5

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.

Kight answered 16/9, 2011 at 15:31 Comment(1)
You need to use an instance of a LinearGradientBrush.. you can't just supply the class name.Nystatin
N
14

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" />
Nichellenichol answered 16/9, 2011 at 15:38 Comment(6)
Thanks for that but I possibly need to ask a different question. I have have the XAML "<TextBox></TextBox>", 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.Kight
Just realised I can do it in "code behind" with "this.TimeInput.BorderBrush = this.HostTextBox.BorderBrush;". Perhaps that's the answer?Kight
you can do that, but thats not really nice. you lose control where you define what. read my editNichellenichol
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.Kight
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.Overland
Found it here: msdn.microsoft.com/en-us/library/cc645061%28v=VS.95%29.aspxOverland

© 2022 - 2024 — McMap. All rights reserved.