本文介绍了你如何保持WPF窗口大小的内容与调整大小后的扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF窗口 SizeToContent =高度。该窗口包含一个<扩展/> 显示最近的活动列表。当膨胀展开窗口增长的大小按比例我想是。当隐藏在窗口中再次按比例调整大小。如果窗口大小的扩展,它的包含列表视图中应增加使用新的空间。 (不介意的颜色有没有帮我算出这个):

I've got a WPF window with SizeToContent="Height". This window contains an <Expander /> that displays a list of recent activity. What I'd like is when the expander is expanded the window grows in size proportionally. When hidden the window again resizes proportionally. If the window is resized the expander and it's contained list view should grow to use the new space. (don't mind the colors there there to help me figure this out):

普通视图

折叠

调整到新的空间

到目前为止,一切进展顺利。问题来了,当&LT;扩展/&GT; 的窗口大小后倒塌。而不是窗口中再次塌陷,列表视图仅仅是隐藏的:

So far this works great. The problem comes when the <Expander /> is collapsed after the window is resized. Rather than the window collapsing again, the list view is simply hidden:

调整大小后折叠

我的直觉告诉我,身高窗口被设置在窗口调整大小,从而覆盖 SizeToContent 财产。所以,我怎么能得到窗口,以保持它的大小为内容的行为,它的大小后?

My intuition tells me that the Height of the window is being set when the window resizes and thus overrides the SizeToContent property. So, how can I get the window to keep it's size to content behavior after it's been resized?

目前XAML:

<Window x:Class="DeployLX.Licensing.Nlm.Admin.v3.DashboardWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Dashboard" Width="504" SizeToContent="Height" Height="275">

  <DockPanel>
    <Menu DockPanel.Dock="Top">
      <MenuItem Header="_File">
        <MenuItem Header="E_xit" Command="{Binding Path=CloseCmd}" />
      </MenuItem>
    </Menu>
    <Grid DockPanel.Dock="Top" Margin="8" ShowGridLines="True">
      <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>

      <Grid Grid.RowSpan="2" Grid.Row="0" Grid.Column="0" Margin="0,0,8,0">
        <Rectangle Fill="Red" />
        <TextBlock>ActiveCount</TextBlock>
      </Grid>

      <Grid Grid.Row="0" Grid.Column="1" Margin="0,0,0,4">
        <Rectangle Fill="Green" />
        <TextBlock>Authorization</TextBlock>
      </Grid>
      <Grid Grid.Row="1" Grid.Column="1" Margin="0,4,0,0">
        <Rectangle Fill="Blue" />
        <TextBlock>Authorization</TextBlock>
      </Grid>
    </Grid>
    <Expander Header="Recent Activity" Margin="8" IsExpanded="True">
      <ListView IsSynchronizedWithCurrentItem="True" MinHeight="100">
        <ListView.View>
          <GridView>
            <GridViewColumn Header="Status"/>
          </GridView>
        </ListView.View>
      </ListView>
    </Expander>
  </DockPanel>
</Window>

更新:我试着听膨胀的倒塌事件,并重新在Windows SizeToContent 属性。这几乎工作。它会导致它再次折叠的窗口,但再次膨胀时,它返回到原来的100像素高度。虽然这是可行的存储和获取这个信息它的气味哈克而且容易出错。

UPDATE: I've tried listening to the Collapsed event of the expander and resetting the Windows SizeToContent property. This almost works. It will cause it to collapse the window again but when expanded again it goes back to the original 100 pixel height. While it's feasible to store and capture this info it smells hacky and prone to errors.

推荐答案

您必须让你的窗口不调整大小,如果你要使用SizeToContent。此外,你不应该使用SizeToContent =高度,然后设置一个明确的高度。想想看 - 这应该相信WPF为窗口的高度,用户的设置或内容?它不能只来回切换在两者之间。

You have to make your window non-resizeable if you're going to use SizeToContent. Also, you shouldn't use SizeToContent="Height", and then set an explicit Height. Think about it - which one should WPF believe for the window height, the user's setting or the Content? It can't just switch back and forth between the two.

这篇关于你如何保持WPF窗口大小的内容与调整大小后的扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:11