6

使用以下 XAML,当您取消选中该复选框时,底行将隐藏。一切都很好,直到你用 gridsplitter 调整它的大小。然后选中/取消选中复选框不会做任何事情。鉴于转换器将高度设置为 0,我希望该行隐藏。这是怎么回事?移动分离器后如何重置高度?

<Grid>
    <Grid.Resources>
        <m:CheckedToLengthConverter x:Key="checkedToLengthConverter" />
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="3*" />
        <RowDefinition Height="{Binding Mode=OneWay, ElementName=ShowBottomCheckBox, Path=IsChecked, Converter={StaticResource checkedToLengthConverter}, ConverterParameter=2}" />
    </Grid.RowDefinitions>
    <Border Background="Blue" />
    <CheckBox Name="ShowBottomCheckBox" IsChecked="True" />
    <GridSplitter HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="5" />
    <Border Background="Red" Grid.Row="1" />
</Grid>

转换器:

public class CheckedToLengthConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((bool)value)
            return new GridLength(int.Parse(parameter.ToString()), GridUnitType.Star);

        return new GridLength(0, GridUnitType.Pixel);
    }

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

2 回答 2

6

问题是,一旦您移动拆分器,第一行将具有明确的宽度,因此将最后一行设置回 * 不会有任何效果。经过一些实验,我想出了下面的代码。请注意,您需要指定 TwoWay 绑定,否则它将不起作用。

public class CheckedToLengthConverter : MarkupExtension, IValueConverter
{
    public GridLength TrueValue { get; set; }
    public GridLength FalseValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return System.Convert.ToBoolean(value) ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }

    #region Overrides of MarkupExtension

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

    #endregion
}

<Grid>
    <Grid.Resources>
        <m:CheckedToLengthConverter TrueValue="2*" FalseValue="*" x:Key="c1" />
        <m:CheckedToLengthConverter TrueValue="3*" FalseValue="0" x:Key="c2" />
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding Mode=TwoWay, Path=IsChecked, 
                       ElementName=ShowBottomCheckBox, 
                       Converter={StaticResource c1}}"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="{Binding Mode=TwoWay, Path=IsChecked, 
                       ElementName=ShowBottomCheckBox, 
                       Converter={StaticResource c2}}"/>
    </Grid.RowDefinitions>
    <Border Background="Blue" />
    <CheckBox Name="ShowBottomCheckBox" IsChecked="True" />
    <GridSplitter HorizontalAlignment="Stretch" 
                  Grid.Row="1" VerticalAlignment="Bottom" Height="5" 
                  ResizeBehavior="PreviousAndNext" />
    <Border Background="Red" Grid.Row="2" />
</Grid>
于 2012-05-17T17:10:21.350 回答
-1

这是 UX 问题的家伙,而不是代码。您尝试做的事情毫无意义,您是否尝试过使用视觉状态?

于 2012-05-18T04:50:06.587 回答