WPF 扩展器和窗口在折叠后调整大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18124829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:23:44  来源:igfitidea点击:

WPF Expander and Window resizing after collapse?

wpf

提问by stenly

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

我已经通过代码隐藏和事件处理程序(在折叠和展开事件上)完成了解决方案。但我认为 WPF 有更好的方法......

Expander XAML implementation:

扩展器 XAML 实现:

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

谢谢指教。

回答by JMK

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:

手动调整窗口大小后,SizeToContent 属性似乎立即失去其值,要解决此问题,只需订阅 Window 的 OnSizeChanged 事件,并重置 SizeToContent 属性:

XAML:

XAML:

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

Code-Behind:

代码隐藏:

private void MainWindow_OnSizeChanged(object sender, SizeChangedEventArgs e)
{
    SizeToContent = SizeToContent.WidthAndHeight;
}