I want to only allow my WPF window to be resized horizontally. How best can I achieve this?
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
- How to properly change a resource dictionary
You could try databinding to the size of the window, and then setting the size back to the old value whenever the vertical dimension is changed.
If you want to use the MinHeight and MaxHeight approach but still allow the window to automatically size itself to fit the size of its content:
To allow automatic content sizing don't use the Loaded event. Use the ContentRendered event instead.
It's kind of a pain. Basically you need to set up a hook function to process windows messages. Then you would catch the WM_SIZING (0x0214) message and modify the parameters so that the horizontal dimension could not be changed.
Pete Brown also has some great info on this topic on his blog.
The following solution allows you to leave
SizeToContent
intact,doesn't require
Win32 Interop
or code behind in your Window:It does rely on Reactive Extensions
In the
Package Manager
run:Then you start by creating a new
Behavior
:Then you add a
UIElement
at the bottom of your Window and apply theBehavior
:Set the following Properties on your Window:
Note: In this example, the user is allowed to resize
Vertically
but notHorizontally
You can easily change the code to allow the opposite, or add properties
to make it configurable.
If you set the
MinHeight
andMaxHeight
attributes of the window to the desired height the window will have a fixed heightBuilding on the answers from User3810621 and others, this can be turned into a reusable behavior which locks in the calculated sizes as min and/or max after the first content render:
The remaining, repetitive values are trivial and omitted for brevity.
Usage: