wpf:Gridsplitter可以在网格之外使用吗?

时间:2009-05-13 18:04:52

标签: wpf layout gridsplitter

我有一个StackPanel,它非常适合用于布置用户在运行时添加的列。但我希望列可以调整大小,我正在阅读有关GridSplitter控件的内容。这就是我想知道的: GridSplitter是winForms拆分器的wpf替代品吗?换句话说,这是允许用户调整窗口区域大小的事实上的方法吗? 它只能在Grid内部使用吗?如果我在stackpanel或dockpanel中有项目,我仍然可以像在WinForms中使用分割器一样使用gridsplitter吗? 如果我必须使用Grid,我怎样才能使其表现得像StackPanel? (希望它不会那样)

3 个答案:

答案 0 :(得分:2)

GridSplitter仅适用于网格,是允许用户调整控件大小的最简单方法。 你想让你的网格(有网格分裂器)表现得像一个堆叠面板是什么意思? stackpanel将完全适合其每个子节点,而带有网格分割器的网格将使其留给用户。

答案 1 :(得分:1)

下面是一个用户控件,允许将项目添加为列。列之间是网格分割器。用户可以单击“删除”按钮删除添加的列,可以使用后面的代码添加列。如果这就是你要找的那个,请告诉我。

用户控制SmartGrid XAML:

<UserControl x:Class="SmartGridDemo.SmartGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid Name="_grid">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    </Grid>
</UserControl>  

用户控制SmartGrid代码:

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SmartGridDemo
{
    public partial class SmartGrid : UserControl
    {
        public SmartGrid()
        {
            InitializeComponent();
        }

        public void Add(UIElement child)
        {
            int columnIndex = _grid.ColumnDefinitions.Count();

            _grid.ColumnDefinitions.Add(
                new ColumnDefinition() 
                {
                    Width = new GridLength(columnIndex == 0 ? 0 :5)
                });

            GridSplitter gridSplitter =
                new GridSplitter()
                {
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment = VerticalAlignment.Stretch,
                    ResizeDirection = GridResizeDirection.Columns,
                    Background = Brushes.Black
                };

            _grid.Children.Add(gridSplitter);
            Grid.SetColumn(gridSplitter, columnIndex);
            Grid.SetRow(gridSplitter, 0);
            Grid.SetRowSpan(gridSplitter, 2);

            columnIndex++;

            _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });

            Button button = new Button();
            button.Content = "Delete";
            button.Tag = new TagTuple() {Child = child, GridSplitter = gridSplitter};
            button.Click += new RoutedEventHandler(DeleteButton_Click);

            _grid.Children.Add(button);
            Grid.SetColumn(button, columnIndex);
            Grid.SetRow(button, 0);

            _grid.Children.Add(child);
            Grid.SetColumn(child, columnIndex);
            Grid.SetRow(child, 1);
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            int columnIndex = Grid.GetColumn(button);
            TagTuple tagTuple = button.Tag as TagTuple;
            _grid.Children.Remove(tagTuple.GridSplitter);
            _grid.Children.Remove(tagTuple.Child);
            _grid.Children.Remove(button as UIElement);

            _grid.ColumnDefinitions.RemoveAt(_grid.ColumnDefinitions.Count() - 1);
            _grid.ColumnDefinitions.RemoveAt(_grid.ColumnDefinitions.Count() - 1);

            foreach (UIElement child in _grid.Children)
            { 
                int columnIndexForChild = Grid.GetColumn(child);
                if (columnIndexForChild > columnIndex)
                {
                    Grid.SetColumn(child, columnIndexForChild - 2);
                }
            }
        }

        private class TagTuple
        {
            public GridSplitter GridSplitter { get; set; }
            public UIElement Child { get; set; }
        }
    }
}

演示代码,在TextBox中添加一些文本,然后点击Add按钮添加新列,XAML:

<Window x:Class="SmartGridDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SmartGridDemo"    
    Title="SmartGridDemo" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <TextBox Name="_texBox" Grid.Row="0" Grid.Column="0" />
        <Button Content="Add" Click="AddButton_Click" Grid.Row="0" Grid.Column="1" />
        <local:SmartGrid x:Name="_smartGrid" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" />
    </Grid>
</Window>

代码背后的演示:

using System;
using System.Windows;
using System.Windows.Controls;

namespace SmartGridDemo
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            _smartGrid.Add(new TextBlock() { Text = "AAA" });
            _smartGrid.Add(new TextBlock() { Text = "BBB" });
            _smartGrid.Add(new TextBlock() { Text = "CCC" });
            _smartGrid.Add(new TextBlock() { Text = "DDD" });
            _smartGrid.Add(new TextBlock() { Text = "EEE" });
            _smartGrid.Add(new TextBlock() { Text = "FFF" });
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            _smartGrid.Add(new TextBlock() { Text = _texBox.Text });
        }
    }
}

答案 2 :(得分:0)

这是一个开源WPF SplitContainer: [http://wpfsplitcontainer.codeplex.com/]