移动GridSplitter后,布局无法正常工作

时间:2013-04-23 05:16:36

标签: wpf tabcontrol gridsplitter

我的目的是:双击TabItemTabControl名为“标签”的TabControl,然后折叠GridSplitter,再展开双击导致。 但是在我移动TabControl并双击要折叠的“标签”之后,TabControl所在列的高度不等于高度GridSplitter,换句话说, <Window x:Class="WpfApplication7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid x:Name="LayoutRoot"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto" MinHeight="20"/> </Grid.RowDefinitions> <Grid Grid.Row="0"> <Grid Grid.Column="1"> <Label> Nothing </Label> </Grid> </Grid> <GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="6" /> <TabControl Grid.Row="2" TabStripPlacement="Bottom" VerticalAlignment="Stretch"> <TabItem Header="Tab" MouseDoubleClick="TabItemDoubleCilck"> <!--ListView--> <ListView Grid.Row="1" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <ListView.View> <GridView> <GridViewColumn Header="AA " Width="100" DisplayMemberBinding="{Binding [0]}" /> <GridViewColumn Header="BB" Width="100" DisplayMemberBinding="{Binding [1]}" /> <GridViewColumn Header="CC" Width="100" DisplayMemberBinding="{Binding [2]}" /> <GridViewColumn Header="DD" Width="100" DisplayMemberBinding="{Binding [3]}" /> </GridView> </ListView.View> <ListViewItem> <x:Array Type="sys:String" > <sys:String>2000/01/01</sys:String> <sys:String>2000/01/01</sys:String> <sys:String>2000/01/01</sys:String> <sys:String>2000/01/01</sys:String> </x:Array> </ListViewItem> <ListViewItem> <x:Array Type="sys:String" > <sys:String>2000/01/01</sys:String> <sys:String>2000/01/01</sys:String> <sys:String>2000/01/01</sys:String> <sys:String>2000/01/01</sys:String> </x:Array> </ListViewItem> </ListView> </TabItem> </TabControl> </Grid> </Window> 不遵循“TabControl”

enter image description here

的.xaml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication7
{
    /// <summary>
    /// MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        const double dBottomTabMinHeight = 20.0;

        private bool isBottomTabUnfold;

        public MainWindow()
        {
            InitializeComponent();
            isBottomTabUnfold = true;
        }

        private void TabItemDoubleCilck(object sender, MouseButtonEventArgs e)
        {
            TabItem tabItm = sender as TabItem;
            if (isBottomTabUnfold)
            {
                ((FrameworkElement)tabItm.Parent).Height = dBottomTabMinHeight;
            }
            else
            {
                ((FrameworkElement)tabItm.Parent).Height = Double.NaN;

            }
            isBottomTabUnfold = !isBottomTabUnfold;
        }
    }
}

的.cs

{{1}}

1 个答案:

答案 0 :(得分:1)

试试这个,希望这是你的预期

    private void TabItemDoubleCilck(object sender, MouseButtonEventArgs e)
    {
        TabItem tabItm = sender as TabItem;
        if (isBottomTabUnfold)
        {
            ((FrameworkElement)tabItm.Parent).Height = dBottomTabMinHeight;
            ((FrameworkElement)tabItm.Parent).VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
        }
        else
        {
            ((FrameworkElement)tabItm.Parent).Height = Double.NaN;
            ((FrameworkElement)tabItm.Parent).VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
        }
        isBottomTabUnfold = !isBottomTabUnfold;
    }

编辑:

然后呢?

    private void TabItemDoubleCilck(object sender, MouseButtonEventArgs e)
    {
        TabItem tabItm = sender as TabItem;
        if (isBottomTabUnfold)
        {
            dUnfoldedHeight = ((FrameworkElement)tabItm.Parent).ActualHeight;
            ((FrameworkElement)tabItm.Parent).Height = dBottomTabMinHeight;
            rowTab.Height = new GridLength(1, GridUnitType.Auto);
        }
        else
        {
            ((FrameworkElement)tabItm.Parent).Height = double.NaN;
            rowTab.Height = new GridLength(dUnfoldedHeight);

        }
        isBottomTabUnfold = !isBottomTabUnfold;
    }

NB:

  • rowTab是第3行定义的名称,dUnfoldedHeight是一个私有成员,可以在折叠之前跟踪展开标签的高度。

  • 当折叠选项卡或向下拖动spliiter直到选项卡看起来像展开时,您可能会发现拖动分割器的奇怪行为,为了解决此问题,您可能需要使用DragDelta和DragCompleted事件分离器决定是否应该将标签视为折叠