2012-06-06 44 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

这工作得很好 - 谢谢。 –