网格布局的Xaml替代方案

网格布局的Xaml替代方案,xaml,grid,hide,row,Xaml,Grid,Hide,Row,我一直在使用网格来保存新应用程序的控件。例如, <Grid Margin="5"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefin

我一直在使用网格来保存新应用程序的控件。例如,

<Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Grid.Column="0" Content="Label1:" />
        <ListBox Grid.Row="0" Grid.Column="1" />                       

        <Label Grid.Row="1" Grid.Column="0" Content="Label2:" />
        <ComboBox Grid.Row="1" Grid.Column="1" />

        <Label Grid.Row="2" Grid.Column="0" Content="Label3:" />
        <TextBox Grid.Row="2" Grid.Column="1" />
    </Grid>

这很好,但是我现在遇到了一种情况,我只想根据第二行组合框中的selectedvalue显示我的第三行

使用网格时,这似乎有点凌乱,将整行的可见性设置为折叠。我想我必须将行内容的高度设置为零

是否有比网格更灵活的布局。我考虑过stackpannel,但不确定是否有多个列并保持行同步


这可能是一个非常简单的问题,但我有兴趣在做任何事情之前从其他人那里获得输入。

我不建议将控件的高度设置为零-首先,仍然可以将tab设置为0高度控件,这至少会让用户感到困惑:)

或者,尝试将任何受影响控件的可见性绑定到组合框选择,例如:

<UserControl xmlns:cnv="clr-namespace:your_namespace_here">
<Grid Margin="5">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="150" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" Grid.Column="0" Content="Label1:" />
    <ListBox Grid.Row="0" Grid.Column="1" />                       

    <Label Grid.Row="1" Grid.Column="0" Content="Label2:" />
    <ComboBox Name="cbo" Grid.Row="1" Grid.Column="1" />

    <Label Grid.Row="2" Grid.Column="0" Content="Label3:" 
        Visibility="{Binding ElementName=cbo, Path=SelectedIndex,
            Converter={cnv:IntToVisibilityConverter}}" />
    <TextBox Grid.Row="2" Grid.Column="1" />
</Grid>
注意,在本例中,如果选择组合中的第一项,转换器将返回Visiblity.Collapsed,否则返回Visiblity.Visible


未经测试的代码,但方法是可靠的。希望这是有用的

我明白你的意思。有道理。非常感谢
System.Windows.Controls.BooleanToVisibilityConverter
也内置在框架中,如果您不需要编写自己的
namespace your_namespace_here
{
public class IntToVisibilityConverter : MarkupExtension, IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int _value = (int)value;
        return (_value > 0) ? Visibility.Visible : Visibility.Collapsed;
    }

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

    #endregion

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}
}