Question

Hey guys I have 3 column expanders, when you have them all open and resize the window they stretch properly to fit the window. When you an close expander, I want it to shrink the window by the width of that expander when it was open. It currently closes the expander back down to 20pixels but the open expanders will be larger (it stretches to fill in the empty space).

Code:

<UserControls:ExpanderWindow x:Class="Lainezor.Windows.ExpanderWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:xt="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended"
    xmlns:pg="clr-namespace:Microsoft.Windows.Controls.PropertyGrid;assembly=WPFToolkit.Extended"
    xmlns:MyUserControls="clr-namespace:Lainezor.UserControls.ExpanderControls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    SizeToContent="WidthAndHeight" Background="AliceBlue" 
    ResizeMode="CanResize" WindowStartupLocation="Manual" Loaded="Window_Loaded">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition>
            <ColumnDefinition.Style>
                <Style TargetType="ColumnDefinition">
                    <Setter Property="Width" Value="20" />
                    <Setter Property="MinWidth" Value="20" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsExpanded, ElementName=Expander1}" Value="True">
                            <Setter Property="Width" Value="20*" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ColumnDefinition.Style>
        </ColumnDefinition>
        <ColumnDefinition Width="2"/>
        <ColumnDefinition>
            <ColumnDefinition.Style>
                <Style TargetType="ColumnDefinition">
                    <Setter Property="Width" Value="20" />
                    <Setter Property="MinWidth" Value="20" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsExpanded, ElementName=Expander2}" Value="True">
                            <Setter Property="Width" Value="20*" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ColumnDefinition.Style>
        </ColumnDefinition>
        <ColumnDefinition Width="2"/>
        <ColumnDefinition>
            <ColumnDefinition.Style>
                <Style TargetType="ColumnDefinition">
                    <Setter Property="Width" Value="20" />
                    <Setter Property="MinWidth" Value="20" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsExpanded, ElementName=Expander3}" Value="True">
                            <Setter Property="Width" Value="20*" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ColumnDefinition.Style>
        </ColumnDefinition>
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Expander Name="Expander1" Grid.Column="0" IsExpanded="True" IsEnabled="True" ExpandDirection="Right">
        <Expander.Header>
            <TextBlock Text="{Binding Source={StaticResource ResourceWrapper}, Path=MyStrings.Expander1Label}"  RenderTransformOrigin="0,0" >
                <TextBlock.LayoutTransform>
                        <RotateTransform Angle="90"/>
                </TextBlock.LayoutTransform>
            </TextBlock>
        </Expander.Header>
        <MyUserControls:UserControl1 x:Name="UserControl1" SizeChanged="UserControl1_SizeChanged"/>
    </Expander>
    <GridSplitter x:Name="gridSplitter" Grid.Column="1" Width="2" Background="#CBE5FE" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext" VerticalAlignment="Stretch" />
    <Expander Grid.Column="2" Grid.RowSpan="2" Name="Expander2" ExpandDirection="Right" Grid.IsSharedSizeScope="True">
        <Expander.Header >
            <TextBlock Text="{Binding Source={StaticResource ResourceWrapper}, Path=MyStrings.Expander2Label}">
                <TextBlock.LayoutTransform>
                        <RotateTransform Angle="90" />              
                </TextBlock.LayoutTransform>
            </TextBlock>
        </Expander.Header>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <MyUserControls:UserControl2 x:Name="UserControl2" HorizontalAlignment="Stretch"/>
        </Grid>
    </Expander>
    <GridSplitter x:Name="gridSplitterTwo" Grid.Column="3" Width="2" Background="#CBE5FE" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext"  VerticalAlignment="Stretch" />
    <Expander Grid.Column="4" Grid.RowSpan="2" Name="Expander3" ExpandDirection="Right">
        <Expander.Header >
            <TextBlock Text="Lainezor">
                <TextBlock.LayoutTransform>
                        <RotateTransform Angle="90" />              
                </TextBlock.LayoutTransform>
            </TextBlock>
        </Expander.Header>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <MyUserControls:UserControl3 x:Name="UserControl3" HorizontalAlignment="Stretch" />
        </Grid>
    </Expander>
</Grid>

Was it helpful?

Solution 2

Attach an event handler to the collapsed event of the expander and subtract the ((Expander)sender).ActualWidth -20 from the window width

OTHER TIPS

Set the SizeToContent of the Window to "WidthAndHeight" and do not set it's Width or Height or designWidth or designHeight.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top