Click here to Skip to main content
15,888,579 members
Articles / Programming Languages / XML

DoubleClickCollapseGridSplitterBehavior

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
9 Sep 2010CPOL 15K   4   1
Answer to a very interesting question on StackOverflow...

Weird name, I know!!!

Found this very interesting question on StackOverflow recently on how to extend a GridSplitter to expand/collapse a grid cell by pressing a button…

“I'm looking to extend the GridSplitter in some way to add a button which when click expands or collapses the control to one of the specified sides of the splitter.”

And I decided to give it a try… But to make it super simple, I will only support collapsing of cells (by double clicking on the GridSplitter) using behaviors (Expression Blend SDK).

Here is my behavior:

C#
public class DoubleClickCollapseGridSplitterBehavior : Behavior<GridSplitter>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        this.AssociatedObject.PreviewMouseDoubleClick += 
		new System.Windows.Input.MouseButtonEventHandler
		(AssociatedObject_PreviewMouseDoubleClick);
    }

    protected override void OnDetaching()
    {
        this.AssociatedObject.PreviewMouseDoubleClick -= 
		new System.Windows.Input.MouseButtonEventHandler
		(AssociatedObject_PreviewMouseDoubleClick); 
        base.OnDetaching();
    }

    void AssociatedObject_PreviewMouseDoubleClick
		(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        GridSplitter splitter = sender as GridSplitter;
        Grid parent = splitter.Parent as Grid;

        int row = (int)splitter.GetValue(Grid.RowProperty);
        int col = (int)splitter.GetValue(Grid.ColumnProperty);

        double splitterHeight = (double)splitter.GetValue(GridSplitter.HeightProperty);
        double splitterWidth = (double)splitter.GetValue(GridSplitter.WidthProperty);

        if (double.IsNaN(splitterWidth))
        {
            parent.RowDefinitions[row].SetValue
		(RowDefinition.HeightProperty, new GridLength(splitterHeight));
        }
        else
        {
            parent.ColumnDefinitions[col].SetValue
		(ColumnDefinition.WidthProperty, new GridLength(splitterWidth));
        }
    }
}

And to use it:

XML
<GridSplitter>
    <i:Interaction.Behaviors>
        <local:DoubleClickCollapseGridSplitterBehavior />
    </i:Interaction.Behaviors>
</GridSplitter>

Very simple and this should also work on Silverlight (not tested yet).

As always, here is the code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
mli280511-Mar-13 8:34
mli280511-Mar-13 8:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.