我怎样才能得到可调节的“;帧”;在WPF中

本文关键字:WPF 调节 | 更新日期: 2023-09-27 18:26:36

我知道在将窗口划分为单独的部分时,我有几个选项可以使用(例如DockPanel、StackPanel等)。我的Windows应用程序要求用户能够在运行时调整不同部分的大小,类似于用户在HTML中调整FRAME宽度的方式。有人对在C#WPF中使用什么类型的控件来实现这一点有建议吗?如果您有任何代码可以说明用户如何将鼠标悬停在剖面边界上并单击"按住"以调整大小,那将是理想的选择。

我怎样才能得到可调节的“;帧”;在WPF中

System.Windows.Controls.GridSplitter

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

示例:

<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>

这里有一个以编程方式在任意方向制作框架的示例。

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;
}