2012-05-17 6 views
5

Con el siguiente XAML, la fila inferior se oculta cuando se desmarca la casilla de verificación. Todo está bien hasta que lo cambies de tamaño con el divisor de grillas. Luego, marcar/desmarcar la casilla de verificación no hace nada. Dado que el convertidor establece la Altura en 0, esperaba que la fila se ocultara. ¿Que esta pasando? ¿Cómo puedo restablecer las alturas después de mover el divisor?¿Cómo restablecer la altura de la fila de la grilla después de usar el divisor?

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

Convertidor:

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

Respuesta

4

El problema es que una vez que se mueve el divisor de la primera fila tendrá una anchura explícita, por lo que el establecimiento de la última fila de nuevo a * no tendrá ningún efecto. Después de un poco de experimentación, obtuve el siguiente código. Tenga en cuenta que debe especificar un enlace de TwoWay o no funcionará.

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

¿Podría dar un ejemplo? No puedo hacer que funcione. – Manuel

+0

@Manuel, he creado algo que funciona para mí. – Phil

-1

Este es el problema de UX chicos, no es el código. Lo que tratas de hacer tiene poco sentido, ¿has intentado usar Visual States en su lugar?

Cuestiones relacionadas