如何在动态布局上使用gridsplitter?

时间:2011-05-02 06:05:31

标签: wpf layout expander gridsplitter

对于我的生活,我无法让gridplitter控制按照我想要的方式行事。

在下面的布局中,我需要每个列表视图右侧的网格浏览器,这将扩展或收缩列表视图并适当调整其他列表视图的大小。

扩展器及其(绿色)面板不应调整大小。

请帮忙!这种控制让我疯狂。

MainWindow.xaml:

<Window x:Class="LayoutTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="400" MinWidth="1000">
    <Grid Name="root" SizeChanged="root_SizeChanged">
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="200" Width="*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition MinWidth="200" Width="*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition MinWidth="200" Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <ListView Grid.Column="0" MinWidth="100">
            <ListViewItem>a</ListViewItem>
            <ListViewItem>b</ListViewItem>
            <ListViewItem>c</ListViewItem>
            <ListViewItem>d</ListViewItem>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="a" Width="100" />
                    <GridViewColumn Header="b" Width="100" />
                    <GridViewColumn Header="c" Width="100" />
                    <GridViewColumn Header="d" Width="100" />
                </GridView>
            </ListView.View>
        </ListView>
        <Expander Name="xleft" Grid.Column="1" ExpandDirection="Left" Expanded="Expanded">
            <Border MinWidth="300"  Width="300" Background="Green" >
                <TextBlock Text="{Binding ElementName=root,Path=ActualWidth}" />
            </Border>
        </Expander>
        <ListView Grid.Column="2" MinWidth="100">
            <ListViewItem>a</ListViewItem>
            <ListViewItem>b</ListViewItem>
            <ListViewItem>c</ListViewItem>
            <ListViewItem>d</ListViewItem>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="a" Width="100" />
                    <GridViewColumn Header="b" Width="100" />
                    <GridViewColumn Header="c" Width="100" />
                    <GridViewColumn Header="d" Width="100" />
                </GridView>
            </ListView.View>
        </ListView>
        <Expander Name="xmiddle" Grid.Column="3" ExpandDirection="Left" Expanded="Expanded">
            <Border MinWidth="300"  Width="300" Background="Green" >
                <TextBlock Text="{Binding ElementName=root,Path=ActualWidth}" />
            </Border>
        </Expander>
        <ListView Grid.Column="4" MinWidth="100">
            <ListViewItem>a</ListViewItem>
            <ListViewItem>b</ListViewItem>
            <ListViewItem>c</ListViewItem>
            <ListViewItem>d</ListViewItem>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="a" Width="100" />
                    <GridViewColumn Header="b" Width="100" />
                    <GridViewColumn Header="c" Width="100" />
                    <GridViewColumn Header="d" Width="100" />
                </GridView>
            </ListView.View>
        </ListView>
        <Expander Name="xright" Grid.Column="5" ExpandDirection="Left" Expanded="Expanded">
            <Border MinWidth="300"  Width="300" Background="Green" >
                <TextBlock Text="{Binding ElementName=root,Path=ActualWidth}" />
            </Border>
        </Expander>
    </Grid>
</Window>

MainWindow.xaml.cs:

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

namespace LayoutTest
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Expander[] expanders;
        Expander priority;

        public MainWindow()
        {
            InitializeComponent();
            expanders = new Expander[] {xleft,xmiddle,xright};
        }

        private void Expanded(object sender, RoutedEventArgs e)
        {
            if (sender is Expander) priority = sender as Expander;
            if (root.ActualWidth > this.ActualWidth)
            {
                foreach (Expander ep in expanders)
                {
                    if (ep.IsExpanded && priority != null && !ep.Name.Equals(priority.Name) )
                    {
                        ep.IsExpanded = false;
                        break;
                    }
                }
            }
        }

        private void root_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Expanded(null,new RoutedEventArgs());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这不是理想的,但这是我要采用的解决方案:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" MinWidth="100" />
        <ColumnDefinition Width="Auto" />

        <ColumnDefinition Width="*" MinWidth="100" />
        <ColumnDefinition Width="Auto" />

        <ColumnDefinition Width="*" MinWidth="100" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0" Background="Green">
    </Border>
    <Expander Name="ex1" Grid.Column="1" ExpandDirection="Left" Expanded="ex_Expanded">
        <Border Background="Blue" MinWidth="200" MaxWidth="200">
        </Border>
    </Expander>
    <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Left" ResizeBehavior="PreviousAndNext" VerticalAlignment="Stretch" Background="Red" />
    <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Right" ResizeBehavior="PreviousAndNext" VerticalAlignment="Stretch" Background="Red" />
    <Border Grid.Column="2" Background="Green">
    </Border>
    <Expander Name="ex2" Grid.Column="3" ExpandDirection="Left" Expanded="ex_Expanded">
        <Border Background="Blue" MinWidth="200" MaxWidth="200">
        </Border>
    </Expander>
    <GridSplitter Grid.Column="3" Width="5" HorizontalAlignment="Left" ResizeBehavior="PreviousAndNext" VerticalAlignment="Stretch" Background="Red" />
    <GridSplitter Grid.Column="3" Width="5" HorizontalAlignment="Right" ResizeBehavior="PreviousAndNext" VerticalAlignment="Stretch" Background="Red" />
    <Border Grid.Column="4" Background="Green">
    </Border>
    <Expander Name="ex3" Grid.Column="5" ExpandDirection="Left" Expanded="ex_Expanded">
        <Border Background="Blue" MinWidth="200" MaxWidth="200">
        </Border>
    </Expander>
</Grid>