(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Using the GridSplitter control in Silverlight

(4 votes)
Nikolay Raychev
>
Nikolay Raychev
Joined Mar 28, 2008
Articles:   22
Comments:   58
More Articles
16 comments   /   posted on Apr 15, 2008
Categories:   Controls

This article is compatible with the latest version of Silverlight.

Introduction

To use the GridSplitter you should be familiar with the Grid control. It is a control that allows the user to resize dynamically the width or height of the Grid cells.

See also:
Grid Article

Overview

The following example demonstrates how to use the GridSplitter:

We want to have two cells whose width can be redistributed. The blue line can be moved left or right when clicking on it, holding the mouse button and dragging. Thus the neighbor cells can be resized.

The XAML code:

<UserControl
    xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
    x:Class="GridSplitter2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" ShowGridLines="True" Background="White" Width="400" Height="300">
        <Grid.RowDefinitions>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <basics:GridSplitter x:Name="grsplSplitter" Grid.Row="0" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Background="Aqua" Width="5"></basics:GridSplitter>
    </Grid>
</UserControl>

Note: you can use a Brush object as a Background property.

Note that the GridSplitter is located in the middle column but not between the columns. It has its own size and the middle column is marked with Width="Auto" to take exactly the width of the GridSplitter. You can add same space around the splitter setting different width of the column holding it. For example:

<Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition Width="25"/>
    <ColumnDefinition />
</Grid.ColumnDefinitions>

 

Here the MinWidth and MaxWidth of the Grid columns become useful. You can restrict the GridSplitter by setting for example the following:

<Grid.ColumnDefinitions>
    <ColumnDefinition MinWidth="50" MaxWidth="250" />
    <ColumnDefinition Width="Auto"/>
    <ColumnDefinition />
</Grid.ColumnDefinitions>

Thus the splitter will continue to work in the same way but you will be able to move it within the width of column – in the above example within 50 and 250 pixels.

You can use the same technique to resize the Height of Grid rows. I won’t describe it in details. I’ll give just an example:

 

<UserControl
    xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
    x:Class="GridSplitter2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" ShowGridLines="True" Background="White" Width="400" Height="300">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <basics:GridSplitter x:Name="grsplSplitter" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Stretch" Background="Aqua" Height="5"></basics:GridSplitter>
    </Grid>
</UserControl>

You can read more about the GridSplitter properties and the dependencies between them at: http://msdn2.microsoft.com/en-us/library/system.windows.controls.gridsplitter(VS.95).aspx

One useful property is the ShowsPreview. When it is set to True and you are moving the mouse only a preview is shown and the splitter is not moving. When the left button is released then the splitter moves.

 

<basics:GridSplitter ShowsPreview="True" x:Name="grsplSplitter" Grid.Row="0" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Background="Aqua" Width="5"></basics:GridSplitter>

You can customize the Style of the preview using the PreviewStyle property.

Conclusion

This article is just a brief description of the key features of the GridSplitter control. It targets the developer who has just started with  the Silverlight controls. Any comments are welcome.

Reference

http://msdn2.microsoft.com/en-us/library/system.windows.controls.gridsplitter(VS.95).aspx


Subscribe

Comments

  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by rishu on Jun 23, 2008 04:11

    Thanks for a nice post , explaing the things in very easy way.

  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by Saket Kumar on Aug 24, 2008 23:00

    Good Article, Concise and to the point

  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by Emiliano on Dec 04, 2008 08:58

    Do you have any idea how to make it work without writing code? I mean, in Blend's designer mode.

    Thanks.

     

  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by Bob on Dec 10, 2008 01:02

    What seems a bit gay is that once you move the grid splitter, the bar becomes blue when it's a horizontal splitter. Perhaps a change of default style would fix that, but it's not a good behavior.

  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by Shashi on Apr 20, 2009 02:07
    I my case splitter is not mooving.what may be the reason?
  • nikolayraychev

    RE: Using the GridSplitter control in Silverlight 2


    posted by nikolayraychev on Apr 21, 2009 02:58
    Shashi, can you post the part of the XAML where your Grid with the GridSplitter is and I'll try to help you.
  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by Riemaxi on Aug 07, 2009 11:15

    Hi

     

    This example is not using any UserControl as Left/Right or Up/Down component. When I use it, it just wont work.

    When I move the splitter the left component wont resize.

  • nikolayraychev

    RE: Using the GridSplitter control in Silverlight 2


    posted by nikolayraychev on Aug 12, 2009 17:18

    Hi Riemaxi,

    I don't see where is the problem using a control in the Left/Right or Up/Down cells of the Grid. Could you please specify what control you are using or give more detailed example.

    Thanks.

  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by k on Dec 17, 2009 01:38
    good
  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by Murali on Jan 30, 2010 01:51

    Is there a way we can set the horizontal split  to be 60% top and 40% bottom with setting the controls height. That way the no matter what the size of the screen is the bottom part is lower than the upper part and the control in those stretch to fill in.

    Thanks in advance.

     

  • nikolayraychev

    RE: Using the GridSplitter control in Silverlight 2


    posted by nikolayraychev on Feb 11, 2010 12:32

    Hi Murali,

    here is your solution:

    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="6*" />
            <RowDefinition Height="Auto" />
            <RowDefinition  Height="4*"/>
        </Grid.RowDefinitions>
        <controls:GridSplitter 
            Grid.Row="1" 
            Height="5" Background="Black"
            VerticalAlignment="Center" HorizontalAlignment="Stretch">
        </controls:GridSplitter>
    </Grid>
  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by Keysle on Mar 12, 2010 21:53
    I'm confused.
    I've tried including this in my xmnls but it fails everytime
    can someone link me to the proper download?
  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by Tweek on Jul 27, 2010 19:30
    Thank, this article is greate!
  • -_-

    RE: Using the GridSplitter control in Silverlight 2


    posted by Mat on Sep 15, 2010 04:12

    thanks good tutorial.

  • -_-

    RE: Using the GridSplitter control in Silverlight


    posted by Hitesh Agja on Feb 22, 2011 20:37
    Cool stuff
  • FlixManuelBritoAmarante

    Re: Using the GridSplitter control in Silverlight


    posted by FlixManuelBritoAmarante on Jan 27, 2012 13:56

    Thank you very much helped me a lot.

Add Comment

Login to comment:
  *      *