Styling GridSplitter wpf C#
Asked Answered
G

5

27

i want to style my GridSplitter like adding dots on it (as found on http://msdn.microsoft.com/en-us/library/aa970265.aspx).

i also want to change gridsplitter color on mouseOver, or apply Aero Theme.

<Style x:Key="GridSplitterStyle1" TargetType="{x:Type GridSplitter}">
  <Setter Property="Background"
          Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
  <Setter Property="PreviewStyle">
    <Setter.Value>
      <Style>
        <Setter Property="Control.Template">
          <Setter.Value>
            <ControlTemplate>
              <Rectangle Fill="#80000000"/>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type GridSplitter}">
        <Border Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<!--Theme-->
<ResourceDictionary.MergedDictionaries>
  <ResourceDictionary 
   Source="/RibbonControlsLibrary;component/Themes/Office2007Blue.xaml" />
</ResourceDictionary.MergedDictionaries>

<GridSplitter x:Name="gridSplitterTreeNodes" Width="10"
              BorderThickness="1,0" Cursor="SizeWE"
              RenderTransformOrigin="-1.2,0.507" ShowsPreview="True" 
              Style="{DynamicResource GridSplitterStyle1}">
  <GridSplitter.Background>
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
      <GradientStop Color="#FFE3EFFF" Offset="0"/>
      <GradientStop Color="#FFAFD2FF" Offset=".45"/>
    </LinearGradientBrush>
  </GridSplitter.Background>
</GridSplitter>
Goliard answered 3/11, 2009 at 7:32 Comment(2)
Giving you sample code... not posting whole code because it is too bigGoliard
I really don't think so that down voters should know any thing about it. They just don't understand due to their lack of knowledge :pGoliard
G
18
<GridSplitter x:Name="gridSplitterTreeNodes" Width="5" BorderThickness="1,0" 
     Cursor="SizeWE" RenderTransformOrigin="-1.2,0.507" ShowsPreview="True"
     Style="{DynamicResource GridSplitterStyle1}">
  <GridSplitter.Background>
    <ImageBrush ImageSource="Images\gripDots.png" TileMode="FlipXY" 
                Stretch="UniformToFill"/>
  </GridSplitter.Background>
</GridSplitter>

You can also save image from Msnd Microsoft to get the same effect, More Info

Goliard answered 10/11, 2009 at 11:35 Comment(1)
Not sure what that "picture" is supposed to be.. the broken image link is pointing to an actual MSDN page. I know this is an old answer, but feel free to fix the link anyway (with stack.imgur uploader?).Upheld
B
42

Mostly for my own future reference, here is a vertical grid splitter that has the rounded shape of a button (but doesn't react to mouseover properly):

<GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Width="8">
    <GridSplitter.Template>
        <ControlTemplate TargetType="{x:Type GridSplitter}">
            <Grid>
                <Button Content="⁞" />
                <Rectangle Fill="#00FFFFFF" />
            </Grid>
        </ControlTemplate>
    </GridSplitter.Template>
</GridSplitter>

A horizontal splitter could just use "····" as the Button's Content.

Barone answered 4/7, 2011 at 7:16 Comment(2)
This looks easy and very clean!Midkiff
Thanks for this. I ended up using "▌▌▌▌▌▌" as the content for a horizontal splitter, which IMHO looks a bit better than "····".Medico
G
18
<GridSplitter x:Name="gridSplitterTreeNodes" Width="5" BorderThickness="1,0" 
     Cursor="SizeWE" RenderTransformOrigin="-1.2,0.507" ShowsPreview="True"
     Style="{DynamicResource GridSplitterStyle1}">
  <GridSplitter.Background>
    <ImageBrush ImageSource="Images\gripDots.png" TileMode="FlipXY" 
                Stretch="UniformToFill"/>
  </GridSplitter.Background>
</GridSplitter>

You can also save image from Msnd Microsoft to get the same effect, More Info

Goliard answered 10/11, 2009 at 11:35 Comment(1)
Not sure what that "picture" is supposed to be.. the broken image link is pointing to an actual MSDN page. I know this is an old answer, but feel free to fix the link anyway (with stack.imgur uploader?).Upheld
T
9

Another way of adding a 'gripper' button/graphic to a GridSplitter, without losing mouse events, would be to use a simple label on top of the splitter.

    <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" Background="Gray"/>

    <Label Grid.Column="1" Content="⁞" Foreground="White" VerticalAlignment="Center" FontSize="26" FontWeight="Bold" IsHitTestVisible="False"/>

Making sure the GridSplitter and Label are in the same Column, and that IsHitTestVisible=False is set in the Label.

Throwaway answered 17/3, 2016 at 9:50 Comment(0)
C
2

For a different type of style you can do the below.

Produces a nice overlaping style. The Gridsplitter overlaps both the left and right content.

<Grid>
     <Grid.ColumnDefinitions>
         <ColumnDefinition Width="*"/>
         <ColumnDefinition Width="1"/>
         <ColumnDefinition Width="*"/>
     </Grid.ColumnDefinitions>

     <Border Grid.Column="0" Background="#777"/>

     <GridSplitter
         Grid.Column="1"
         HorizontalAlignment="Stretch" 
         ResizeDirection="Columns"
         ResizeBehavior="PreviousAndNext"
         Panel.ZIndex="2">
           <GridSplitter.Template>

               <ControlTemplate TargetType="{x:Type GridSplitter}">

                   <Grid>
                        <Rectangle IsHitTestVisible="False" Fill="Black"/>
                        <Border
                           Background="White"
                           Width="25" Height="25" c 
                           CornerRadius="25" Margin="-13 0">
                           <Path Stroke="Black" StrokeThickness="0.5" Width="17" Height="7" Data="m 4.4549201,1048.4664 -4.33056515,1.9095 4.33056515,1.9094 0,-3.8189 z m 3.0901599,0 0,3.8189 4.330565,-1.9094 -4.330565,-1.9095 z m -3.2239449,0.2053 0,3.4083 -3.86518514,-1.7041 3.86518514,-1.7042 z m 3.3577349,0 3.865185,1.7042 -3.865185,1.7041 0,-3.4083 z" Stretch="Fill"/>
                        </Border>
                   </Grid>

                </ControlTemplate>

                </GridSplitter.Template>

     </GridSplitter>

     <Border Grid.Column="2" Background="#777"/>

</Grid>

Sample Output

Constantin answered 6/11, 2020 at 21:31 Comment(0)
F
1

In response to Burton Radons's answer, I personally prefer the styling:

<GridSplitter
  Width="8"
  HorizontalAlignment="Stretch"
  VerticalAlignment="Stretch">

  <GridSplitter.Template>
    <ControlTemplate TargetType="{x:Type GridSplitter}">
      <Grid>
        <TextBlock
          HorizontalAlignment="Center"
          VerticalAlignment="Center"
          Text="⁞" />
        <Rectangle Fill="#00FFFFFF" />
      </Grid>
    </ControlTemplate>
  </GridSplitter.Template>
</GridSplitter>

This implementation produces the same aesthetic effect whilst also maintaining functionality.

Fluorescence answered 12/9, 2020 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.