크기 조정 후 확장기를 사용하여 WPF 창 크기를 콘텐츠에 맞게 유지하는 방법
나는 WPF 창이 SizeToContent="Height"
. 이 창에는 <Expander />
최근 활동 목록을 표시하는 이 포함되어 있습니다 . 내가 원하는 것은 확장기가 확장되면 창 크기가 비례 적으로 커지는 것입니다. 숨겨지면 창 크기가 비례 적으로 다시 조정됩니다. 창 크기가 조정되면 확장기 및 포함 된 목록보기가 새 공간을 사용하도록 확장되어야합니다. (내가 이것을 알아내는 데 도움이되는 색상에 신경 쓰지 마십시오) :
일반보기
대체 텍스트 http://www.deploylx.com/so/wpfexpander/Open.png
접힘
대체 텍스트 http://www.deploylx.com/so/wpfexpander/Closed.png
새로운 공간으로 크기 조정
대체 텍스트 http://www.deploylx.com/so/wpfexpander/Expanded.png
지금까지 이것은 훌륭하게 작동합니다. <Expander />
창 크기가 조정 된 후가 축소 되면 문제가 발생합니다 . 창이 다시 축소되는 대신 목록보기가 숨겨집니다.
크기 조정 후 축소됨
대체 텍스트 http://www.deploylx.com/so/wpfexpander/Collapsed.png
내 직감에 Height
따르면 창의 크기가 조정되어 SizeToContent
속성 이 재정의 될 때 창의가 설정됩니다 . 그렇다면 창 크기를 조정 한 후 콘텐츠 동작에 맞게 창을 유지하려면 어떻게해야합니까?
현재 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>
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.
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.
The easiest way to cope is to take manual resizing out of the equation by setting ResizeMode="NoResize"
on the window. However, if you have WindowStyle="None"
I've noticed that on Vista Aero this causes the window to completely shed the "chrome" and the window looks awkward. Also, this somewhat of a cop out since it looks like you want to give the user resizing capabilities.
The problem is that you have two conflicting goals: 1.) you always want SizeToContent="Height"
when collapsing the expander control, 2.) you want SizeToContent="Height"
when expanding the expander control unless the user has manually resized the window (SizeToContent="Manual"), in which case you'd like to return to the user's manual height.
Yikes. I don't think you can get around saving the expanded height yourself. WPF won't remember the user's manual height upon expanding once you've reverted back to SizeToContent="Height"
in your collapsed event handler.
Try this, this should fit your needs: SizeToContent="WidthAndHeight" Height="Auto" Width="Auto"
As I discovered in my question, setting the Height to Double.NaN causes it to reset to SizeToContent happiness. I don't know if it will remember the size of your expander though. You might try Kent's Resizer control to move the sizing behavior to the expander rather than the containing window.
I've found that putting the body of my Window in a View and then putting the view as the sole child in the window solved similar problems...
가장 바람직한 솔루션은 아니지만 작동하는 것 같습니다.
'programing' 카테고리의 다른 글
PHP : 세션의 기본 수명은 얼마입니까? (0) | 2021.01.15 |
---|---|
왜 True / False가 파이썬에서 대문자로 쓰이나요? (0) | 2021.01.15 |
Runtime.exec를 호출 할 때 stdout 캡처 (0) | 2021.01.15 |
파이썬에서 HTML 구문 분석-lxml 또는 BeautifulSoup? (0) | 2021.01.15 |
뮤텍스는 변경 가능해야합니까? (0) | 2021.01.15 |