I am trying to set the MinHeight / MinWidth of a resizeable window based on its initial height (when SizeToContent="WidthAndHeight").
I have seen a couple of answers / solutions:
http://weblogs.asp.net/psheriff/archive/2010/01.aspx
Set form MinWidth and MinHeight based on child property
However:
I am trying to use the MVVM pattern and would like to be able to achieve this in the xaml.
I would also like to keep values such as MinHeight out of the ViewModel - I dont think they belong there as they tie a trivial part of the behaviour of the view to the viewmodel. This is something I would like to leave to the UX designer.
A solution I am struggling with is to use the following xaml / binding:
<Window
....
x:Name="mainWindow"
SizeToContent="WidthAndHeight"
ResizeMode="CanResizeWithGrip"
MinHeight="{Binding ElementName=mainWindow, Mode=OneTime, Path=ActualHeight}"
>
I would hope that 'Mode=OneTime' would bind the MinHeight to the initial value of the windows height.
But it does not work..
Can someone please explain why? Is there a solution that meets my criteria?
Thanks,
Mark
Your code:
It will not work, because the default value of
ActualHeightis zero, and by the time WPF framework resizes your window, it already have assignedMinHeightwith the default value ofActualHeightwhich is zero!First thing you can try is this: change
Mode=OneTimetoMode=Default, so that WPF could updateMinHeightwhenActualHeightgets changed on resizing the window. If that works, then you'll be happy.Otherwise, you've to handle the
SizeChangedevent, and in the handler you can update theMinHeight.In the code-behind:
Hope it will solve your problem. Or atleast will give you some idea as to how to proceeed. If you want to fix the size of your window, then you can also set the
MaxHeightin theWindow_SizeChanged()handler.XAML ONLY SOLUTION