2012-05-17 12 views
5

Con il seguente XAML la riga inferiore si nasconde quando si deseleziona la casella di controllo. Tutto va bene finché non lo ridimensionate con il gridsplitter. Quindi selezionare/deselezionare la casella di controllo non fa nulla. Dato che il convertitore imposta l'altezza a 0, mi aspettavo che la riga si nascondesse. Cosa sta succedendo? Come posso ripristinare le altezze dopo aver spostato lo splitter?Come ripristinare l'altezza della riga della griglia dopo aver utilizzato lo splitter?

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

Converter:

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

risposta

4

Il problema è che una volta che si sposta il divisore prima fila avrà una larghezza esplicita, in modo da impostare l'ultima fila di nuovo a * non avrà alcun effetto. Dopo alcune sperimentazioni ho trovato il codice qui sotto. Si noti che è necessario specificare un binding TwoWay o che non funzionerà.

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

Potresti fare un esempio? Non posso farlo funzionare. – Manuel

+0

@Manuel, ho creato qualcosa che funziona per me. – Phil

-1

Questo è un problema UX, non il codice. Quello che stai cercando di fare ha poco senso, hai provato a usare Visual States invece?

Problemi correlati