How do you keep the WPF window sized to content with an Expander after resize

前端 未结 5 539
伪装坚强ぢ
伪装坚强ぢ 2021-01-31 14:07

I\'ve got a WPF window with SizeToContent=\"Height\". This window contains an that displays a list of recent activity. What I\'d lik

相关标签:
5条回答
  • 2021-01-31 14:21

    Try this, it should fit your needs:

    <Window ...
            SizeToContent="WidthAndHeight"  
            Height="Auto" 
            Width="Auto">
    
    0 讨论(0)
  • 2021-01-31 14:26

    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.

    0 讨论(0)
  • 2021-01-31 14:32

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

    Not the most desirable solution, but it seems to work.

    0 讨论(0)
  • 2021-01-31 14:36

    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.

    0 讨论(0)
  • 2021-01-31 14:40

    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.

    0 讨论(0)
提交回复
热议问题