Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Expander and Window resizing after collapse?

Tags:

wpf

I am dealing with one problem regarding to main window resizing after the expander is collapsed and window was manually resized to bigger size by drag/drop before that. The expander is collapsed but the main window is not resized to "fit" the inner component size.

The components are placed to auto sized grid columns and main window is set to WidhtAndHeight size content.

Main window property set:

SizeToContent="WidthAndHeight"

I have done solution via code behind and event handlers (on collapse and expand events). But I think the WPF has better approach ...

Expander XAML implementation:

<Expander Name="expander" Grid.Column="2" Grid.Row="0" Grid.RowSpan="5" ExpandDirection="Right" ToolTip="Expand window for history details" Expanded="Expander_Expanded" Collapsed="Expander_Collapsed" SizeChanged="expander_SizeChanged" >
        <Expander.Header>
            <TextBlock RenderTransformOrigin="0.3,1.5">
                    <TextBlock.RenderTransform>
                        <TransformGroup>
                            <RotateTransform Angle="90" />
                        </TransformGroup>
                    </TextBlock.RenderTransform>
                    Show history
                </TextBlock>
        </Expander.Header>
        <StackPanel>               
            <ListView ItemsSource="{Binding Path=HistoryList}" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" MaxHeight="350" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Operation" DisplayMemberBinding="{Binding Operation}" />
                    <GridViewColumn Header="Result" DisplayMemberBinding="{Binding Result}" />
                </GridView>
            </ListView.View>
        </ListView>
        </StackPanel>
    </Expander>

Does anyone have a clue how to intelligent solve this case?

Thanks for advise.

like image 569
st35ly Avatar asked Aug 08 '13 11:08

st35ly


1 Answers

The SizeToContent property seems to lose its value as soon as you resize the Window manually, to fix this just subscribe to the OnSizeChanged event of your Window, and reset the SizeToContent property:

XAML:

<Window SizeToContent="WidthAndHeight" SizeChanged="MainWindow_OnSizeChanged">

Code-Behind:

private void MainWindow_OnSizeChanged(object sender, SizeChangedEventArgs e)
{
    SizeToContent = SizeToContent.WidthAndHeight;
}
like image 122
JMK Avatar answered Oct 07 '22 11:10

JMK