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> </Window>
Nawaz
source share