2012-06-06 43 views
0

我有一些網格有一些行。行的高度相對設置窗口大小是這樣的:基於內容的可見性隱藏網格行

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="0.3*" /> 
     <RowDefinition Height="0.2*" /> 
     <RowDefinition Height="0.2*" /> 
     <RowDefinition Height="0.1*" /> <!-- hide this row --> 
     <RowDefinition Height="0.2*" /> 
    </Grid.RowDefinitions> 
</Grid> 

現在我想隱藏一個行的基礎上綁定屬性的內容。因此,我將內容對象的Visiblity屬性設置爲Collapsed。內容的Visiblity工作正常,但行仍然需要原始空間。

當內容的Visiblity被摺疊時,有沒有辦法隱藏行?注意:我不想將Height設置爲RowDefinitionAuto,因爲我無法將Height設置爲相對於窗口大小,並且行的高度被調整爲行內部內容的高度。

回答

1

您可以將行的Height屬性綁定到綁定屬性。

然後,您需要從typeof(綁定屬性)到System.Windows.GridLength的轉換器(實現IValueConverter)。

也許類似

[ValueConversion(typeof(System.Windows.Visibility), typeof(System.Windows.GridLength))] 
public class VisibToHeightConv : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     bool b = (boolean)value; 

     if (b == true) 
      return new System.Windows.GridLength(0, System.Windows.GridUnitType.Star); 
     else 
      return new System.Windows.GridLength(80, System.Windows.GridUnitType.Star); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

綁定的屬性只是一個布爾值來設置可見性(用布爾以公開程度轉換器)。 行的高度應取決於窗口的高度。 –

+0

看看轉換器的行高 – Klaus78

+0

這工作得很好 - 謝謝。 –