2011-05-10 75 views
6

一旦使用網格劃分器調整網格大小,當其他行摺疊時,行*將不會回收空間。調整大小後的WPF GridSplitter

我有三行的主詳細視圖中的以下網格。數據網格位於中間的分離器頂部,最後一行爲內容控制視圖。分離器上有一個關閉按鈕來摺疊細節。這一切都適用於一旦用戶使用網格分割器調整大小的例外。

<Grid Margin="3,0"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Style="{StaticResource CollapsableRow}"/><!-- Splitter Here --> 
     <RowDefinition Style="{StaticResource CollapsableRow}"/> 
    </Grid.RowDefinitions> 

的GridSplitter風格:

<Style x:Key="gridSplitterStyle" TargetType="{x:Type GridSplitter}"> 
    <Setter Property="Visibility" Value="{Binding IsItemSelected, Converter={StaticResource BoolToShow},ConverterParameter='Visible|Collapsed'}" /> 
    <Setter Property="Width" Value="Auto"/> 
    <Setter Property="Height" Value="14"/> 
    <Setter Property="HorizontalAlignment" Value="Stretch"/> 
    <Setter Property="Border.BorderBrush" Value="#FF6593CF" /> 
    <Setter Property="Border.BorderThickness" Value="0,1,0,0" /> 
    <Setter Property="UIElement.SnapsToDevicePixels" Value="True" /> 
    <Setter Property="UIElement.Focusable" Value="False" /> 
    <Setter Property="Control.Padding" Value="7,7,7,7" /> 
    <Setter Property="Cursor" Value="SizeNS" /></Style> 

就像我說的崩潰正常工作,除非gridsplitter來調整。之後,空白停留。編輯: H.B.和codenaked有簡單洽建議,以便和我試圖執行這些W/O的數據觸發成功:

<Style x:Key="CollapsableRow" TargetType="{x:Type RowDefinition}"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding SelectedItem, Converter={StaticResource IsNullConverter}}" Value="True"> 
      <Setter Property="RowDefinition.Height" Value="0"/> 
     </DataTrigger> 
     <DataTrigger Binding="{Binding SelectedItem, Converter={StaticResource IsNullConverter}}" Value="False"> 
      <Setter Property="RowDefinition.Height" Value="Auto"/> 
     </DataTrigger>    
    </Style.Triggers> 
</Style> 

回答

6

由於網格分離器和細節已被隱藏可見性是重置下一行定義高度的明顯選擇。

/// <summary> 
/// Grid splitter that show or hides the following row when the visibility of the splitter is changed. 
/// </summary> 
public class HidableGridSplitter : GridSplitter { 

    GridLength height; 

    public HidableGridSplitter() 
    { 
     this.IsVisibleChanged += HideableGridSplitter_IsVisibleChanged; 
     this.Initialized += HideableGridSplitter_Initialized; 
    } 

    void HideableGridSplitter_Initialized(object sender, EventArgs e) 
    { 
     //Cache the initial RowDefinition height, 
     //so it is not always assumed to be "Auto" 
     Grid parent = base.Parent as Grid; 
     if (parent == null) return; 
     int rowIndex = Grid.GetRow(this); 
     if (rowIndex + 1 >= parent.RowDefinitions.Count) return; 
     var lastRow = parent.RowDefinitions[rowIndex + 1]; 
     height = lastRow.Height; 
    } 

    void HideableGridSplitter_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     Grid parent = base.Parent as Grid; 
     if (parent == null) return; 

     int rowIndex = Grid.GetRow(this); 

     if (rowIndex + 1 >= parent.RowDefinitions.Count) return; 

     var lastRow = parent.RowDefinitions[rowIndex + 1]; 

     if (this.Visibility == Visibility.Visible) 
     { 
      lastRow.Height = height; 
     } 
     else 
     { 
      height = lastRow.Height; 
      lastRow.Height = new GridLength(0); 
     } 

    } 
1

如果使用GridSplitter高地不再Auto但具體值。您需要使用樣式或事件以及代碼背後的方式手動更改值這雙擊重置了一個自動調整列:

private void ColumnSplitter_DoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    if (!ColumnTreeView.Width.IsAuto) ColumnTreeView.Width = new GridLength(); 
} 
+0

中類似問題的回答。在代碼中設置網格長度是需要完成的工作。但是在這種情況下雙擊不起作用。網格分路器正在設計風格,實際上是分路器上的一個按鈕,用於完成關閉。我選擇覆蓋網格分割器並在那裏設置parent.hiegth。 – 2011-05-12 09:56:59

+0

我的代碼只是一個例子,我從來沒有想過你會對DoubleClick做任何事情。 – 2011-05-12 13:11:25

0

根據您提供什麼,該GridSplitter將調整前面和後面的行。您可以通過此代碼看到此操作:

<Grid Margin="3,0"> 
    <Grid.RowDefinitions> 
     <RowDefinition x:Name="row0" Height="*" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition x:Name="row2" Height="Auto" /> 
    </Grid.RowDefinitions> 
    <Border Background="Red" > 
     <TextBlock Text="{Binding ElementName=row0, Path=Height}" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
    </Border> 
    <GridSplitter Grid.Row="1" Style="{StaticResource gridSplitterStyle}" HorizontalAlignment="Stretch" /> 
    <Border Background="Blue" Grid.Row="2" MinHeight="50"> 
     <TextBlock Text="{Binding ElementName=row2, Path=Height}" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
    </Border> 
</Grid> 

最後一行的大小實際上會從Auto更改爲固定高度。因此,即使您摺疊該行中的內容,它仍會佔用指定的空間。您需要將該行重置爲Height="Auto"才能真正將其摺疊到內容中。

+0

我喜歡你和H.B.並建議在該行觸發一個樣式觸發器: – 2011-05-11 11:21:39

+0

沒有工作:(因爲rowDefinition需要設置GridLength的一個新實例 – 2011-05-12 09:58:41

+0

@Ryan - 不知道我明白如果將this.row2 .Height = GridLength.Auto;'然後它會重置它。 – CodeNaked 2011-05-12 11:52:33

相關問題