Doug
Doug

Reputation: 5266

How can I get adjustable "frames" in WPF?

I know I have several options to use when it comes to dividing up my window into separate sections (e.g., DockPanel, StackPanel, etc). My Windows application requires that the user be able to adjust the size of different sections at runtime, similar to how a user can adjust FRAME widths in HTML. Does anyone have suggestions for what type/types of controls to use to accomplish this in C# WPF? If you have any code that would illustrate how a user can mouse-over a section boundary and click-hold to adjust the size, that would be ideal.

Upvotes: 3

Views: 2823

Answers (2)

Gabe Halsmer
Gabe Halsmer

Reputation: 868

Here's an example of making frames programmatically, in either direction.

var topTB = new TextBox();
var middleTB = new TextBox();
var bottomTB = new TextBox();

var g = MakeSideBySideFrames(this.Root, topTB, middleTB, bottomTB);
g.Height = 300.0;

public Grid MakeTopBottomFrames(Panel parent, params UIElement[] items)
{
    return MakeFrames(parent, 
        newPosition: (g, len)    => { g.   RowDefinitions.Add(new RowDefinition    { Height = len }); },
        setPosition: (item, inx) => { Grid.SetRow(item, inx); },
        items: items);
}
public Grid MakeSideBySideFrames(Panel parent, params UIElement[] items)
{
    return MakeFrames(parent, 
        newPosition: (g, len)    => { g.ColumnDefinitions.Add(new ColumnDefinition { Width = len }); },
        setPosition: (item, inx) => { Grid.SetColumn(item, inx); },
        items: items);
}
Grid MakeFrames(Panel parent, 
        Action<Grid, GridLength> newPosition,
        Action<UIElement, int> setPosition,
        params UIElement[] items
    )
{
    var g = new Grid();
    parent.Children.Add(g);

    for (var inx = 0; inx < items.Length; inx++)
    {
        if (inx > 0)
        {
            newPosition(g, new GridLength(5));

            var gs = new GridSplitter();
            g.Children.Add(gs);
            setPosition(gs, (inx * 2) - 1);
            gs.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            gs.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            gs.Background = new SolidColorBrush(Colors.Black);
            gs.ShowsPreview = true;
        }

        newPosition(g, new GridLength(1, GridUnitType.Star));
        g.Children.Add(items[inx]);
        setPosition(items[inx], inx * 2);
    }

    return g;
}

Upvotes: 0

Eric Dahlvang
Eric Dahlvang

Reputation: 8292

System.Windows.Controls.GridSplitter

http://msdn.microsoft.com/en-us/library/system.windows.controls.gridsplitter.aspx

Example:

<Grid VerticalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="5" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <ListBox Grid.Row="0" >
        <TextBlock>Hello</TextBlock>
    </ListBox>

    <GridSplitter Grid.Row="1" 
         Height="5" Background="Gray" 
         VerticalAlignment="Top" 
         HorizontalAlignment="Stretch" />

    <ListBox Grid.Row="2" >
        <TextBlock>World</TextBlock>            
    </ListBox>
</Grid>

Upvotes: 7

Related Questions