0

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

<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>

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

使用网格,这似乎有点混乱,也将整行的可见性设置为折叠。我想我必须通过将行内容的高度设置为零来做到这一点。

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

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

4

1 回答 1

1

我不建议将控件的高度设置为零 - 一方面,仍然可以将选项卡设置为 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>

在代码中,将返回相关可见性类型的转换器放在一起:

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;
    }
}
}

请注意,在此示例中,如果选择了组合中的第一项,则转换器将返回 Visiblity.Collapsed,否则返回 Visiblity.Visible。

未经测试的代码,但方法是合理的。希望这有点用!

于 2010-01-19T12:42:21.060 回答