Giving a stackPanel the same LinearGradientBrush as the toolbar has by default

1.9k Views Asked by At

I have two ToolBars side by side (I had to do that so I could align buttons right and left) But now, I have to add some text that can be aligned right left or center between both toolbars. I added the text in a TextBlock inside a StackPanel disposed on a Grid at the second position (between the toolbars) and, of course, it created a gap between both toolbars (A stackPanel doesn't have the same style as a toolbar, of course).

I would like to replicate the toolbar LinearGradientBrush on my stackpanel so it looks the same as my toolbar does. the point is to make the thing look like ONE toolbar.

Is there a way to get the ToolBar Style or to recreate it with a LinearGradientBrush definition, and if so, how?

3

There are 3 best solutions below

1
On BEST ANSWER

Here's one way you could do it. Say one of your toolbars' name is "toolBar." Bind the Background property on the StackPanel to the Background property on the ToolBar as such:

<StackPanel Background="{Binding Path=Background, ElementName=toolBar}" />

Hope that helps! :)

EDIT:

You can check out the control template for the ToolBar here.

The LinearGradientBrush used looks as such:

<LinearGradientBrush x:Key="DarkBrush" StartPoint="0,0" EndPoint="0,1">
  <GradientBrush.GradientStops>
    <GradientStopCollection>
      <GradientStop Color="#FFF" Offset="0.0"/>
      <GradientStop Color="#AAA" Offset="1.0"/>
    </GradientStopCollection>
  </GradientBrush.GradientStops>
</LinearGradientBrush>

In the event you just wanted to use this instead of binding. :)

1
On

The default templates and styles are available on MSDN, you could probably extract the relevant bits from there.

0
On

I found an issue when doing this exact same thing in an application. When running the same application on an XP machine as I did a Vista machine, I found that there was a 2 pixel difference in the height between the two toolbars.

To get around this, I ended up creating one StackPanel which housed the two tool bar objects (setting the background to be transparent).

This ensured that the look and feel was the same between the two OS's (at the time the company was running both) and will also aid you in the event that one of your toolbars grow in height without the other.