2013-04-23 38 views
0

我的目的是:雙擊TabControl中名爲「Tab」的TabItem,然後TabControl將被摺疊,另一個雙擊導致展開。 但是,在我移動GridSplitter並雙擊要摺疊的「Tab」後,它不能正確工作,即TabControl所在列的高度不等於TabControl的高度,換句話說, GridSplitter不要按照「TabControl」移動GridSplitter後,佈局無法正常工作

enter image description here

的.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; 
     } 
    } 
} 
+0

請檢查我的編輯答案 – dnr3 2013-04-23 06:51:07

回答

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是第三rowdefinition的名字,dUnfoldedHeight是跟蹤展開標籤的高度它摺疊前的私有成員。

  • 您可能會發現一個奇怪的行爲,當選項卡被摺疊時拖動分隔符或拖動spliiter直到該選項卡看起來像展開時,要解決此問題,您可能必須使用DragDelta和DragCompleted事件分配器來決定是否最終選項卡應被視爲摺疊

+0

對不起,'GridSplitter'仍然保持不變。我只是希望它沿着'TabControl'向上和向下。 – SubmarineX 2013-04-23 06:26:21

+0

非常感謝你。 – SubmarineX 2013-04-23 07:00:04