Search code examples
wpfstylestoolbarstackpanellineargradientbrush

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


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?


Solution

  • 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. :)