崩溃后WPF扩展器和窗口大小调整?

时间:2022-08-19 21:38:57

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.

组件放置在自动调整大小的网格列中,主窗口设置为WidhtAndHeight大小内容。

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.

谢谢你的建议。

1 个解决方案

#1


3  

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:

一旦手动调整Window大小,SizeToContent属性似乎就会丢失它的值,修复它只是订阅Window的OnSizeChanged事件,并重置SizeToContent属性:

XAML:

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

Code-Behind:

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

#1


3  

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:

一旦手动调整Window大小,SizeToContent属性似乎就会丢失它的值,修复它只是订阅Window的OnSizeChanged事件,并重置SizeToContent属性:

XAML:

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

Code-Behind:

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