none
Expander expand automatically

    Question

  • Hi, in wpf is there a way to expand an expander automatically if the grid that the control is parent to, resized more than a value

    thanks.


    Monday, February 08, 2016 5:40 PM

Answers

  • Hi,

    you could use a ValueConverter for this:

    Xaml: [Note that I bound to the Windows Height for conveniance for this example]

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication1" 
            mc:Ignorable="d"  DataContext="{Binding RelativeSource={RelativeSource Self}}" 
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:HeightToBooleanConverter x:Key="conv"></local:HeightToBooleanConverter>
        </Window.Resources>
        <Grid x:Name="grid1" Margin="0" Background="Beige" VerticalAlignment="Stretch">
            <Expander x:Name="exp" IsExpanded="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=Height, Converter={StaticResource conv}, ConverterParameter=400.0}">
                <Rectangle Width="400" Height="250" Fill="Black"></Rectangle>
            </Expander>
        </Grid>
    </Window>

    and the Converter like:

        class HeightToBooleanConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                double d = (double)value;
                if (!double.IsNaN(d))
                {
                    double i = 0;
    
                    double.TryParse(parameter.ToString(), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i);
    
                    bool f = d >= i;
                    return f;
                }
                else
                    return false;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return null;
            }
        }

    Regards,

      Thorsten


    Monday, February 08, 2016 8:30 PM