WPF - an attempt to set MinHeight with a resizable window based on its initial height

I am trying to set the MinHeight / MinWidth of the resizable window based on its initial height (when SizeToContent = "WidthAndHeight").

I saw a couple of answers / solutions:

http://weblogs.asp.net/psheriff/archive/2010/01.aspx

Set the MinWidth and MinHeight form based on the child property

But:

  • I am trying to use the MVVM pattern and would like to be able to achieve this in xaml.

  • I would also like to preserve values ​​such as MinHeight from the ViewModel - I don't think they belong there, as they relate the trivial part of the look behavior of the ViewModel. This is what I would like to leave to the UX designer.

The solution I'm struggling with is to use the following haml / binding:

<Window .... x:Name="mainWindow" SizeToContent="WidthAndHeight" ResizeMode="CanResizeWithGrip" MinHeight="{Binding ElementName=mainWindow, Mode=OneTime, Path=ActualHeight}" > 

I hope that "Mode = OneTime" will bind MinHeight to the initial window height value.

But that does not work.

Can someone explain why? Is there a solution that meets my criteria?

Thanks,

Mark

+7
source share
2 answers

Your code:

 <Window .... x:Name="mainWindow" SizeToContent="WidthAndHeight" ResizeMode="CanResizeWithGrip" MinHeight="{Binding ElementName=mainWindow, Mode=OneTime, Path=ActualHeight}" > 

This will not work, because the default value of ActualHeight is zero, and by the time WPF changes the size of your window, it has already assigned MinHeight with the default ActualHeight , which is zero!

The first thing you can try is: change Mode=OneTime to Mode=Default so that WPF can update MinHeight when ActualHeight changes when the ActualHeight is resized. If this works, then you will be happy.

Otherwise, you must handle the SizeChanged event, and in the handler you can update MinHeight .

 <Window .... x:Name="mainWindow" SizeToContent="WidthAndHeight" ResizeMode="CanResizeWithGrip" SizeChanged="Window_SizeChanged" > 

In code:

 bool firstTime= true; private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { FrameworkElement element = sender as FrameworkElement; if ( firstTime) { element.MinHeight = e.NewSize.Height; firstTime= false; } } 

Hope this solves your problem. Or at least you will give you some idea of ​​how to ask. If you want to fix the size of your window, you can also set MaxHeight in the Window_SizeChanged() handler.


XAML SOLUTION ONLY

 <Window x:Name="mainWindow" SizeToContent="WidthAndHeight" ResizeMode="CanResizeWithGrip" > <Window.Triggers> <EventTrigger RoutedEvent="SizeChanged"> <BeginStoryboard> <Storyboard Storyboard.TargetName="mainWindow"> <DoubleAnimation Storyboard.TargetProperty="MinHeight" To="{Binding ElementName=mainWindow, Path=ActualHeight}"/> </Storyboard> </BeginStoryboard> </EventTrigger> </Window.Triggers> <!---- other code goes here---> </Window> 
+8
source

I suspect you are looking for this piece of code:

 <Window x:Name="myWindow" MinHeight="266" Height="{Binding ElementName=myWindow, Path=MinHeight}" MinWidth="480" Width="{Binding ElementName=myWindow, Path=MinWidth}"> 

Sets the height and width in the MinHeight and MinWidth settings. Thus, you can only change one position.

0
source

All Articles