0

我的目的是:双击TabItem名为“Tab”中的“Tab” TabControl,然后TabControl将其折叠起来,再双击导致展开。但是在我移动GridSplitter并双击要折叠的“Tab”之后,它不能正常工作,列的高度TabControl不等于列的高度,TabControl换句话说,GridSplitter不要跟随“选项卡控件”

在此处输入图像描述

.xaml

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

。CS

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

1 回答 1

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

注意:

  • rowTab 是第 3 个行定义的名称,dUnfoldedHeight 是一个私有成员,用于在折叠之前跟踪展开的选项卡的高度。

  • 您可能会发现在选项卡折叠时向上拖动拆分器或向下拖动拆分器直到选项卡看起来像展开时会出现奇怪的行为,要解决此问题,您可能必须使用拆分器的 DragDelta 和 DragCompleted 事件来决定是否最后标签应被视为折叠与否

于 2013-04-23T06:01:00.550 回答