How to link Listview MaxHeight with current window height?

How to link Listview MaxHeight with current window height?

I would like to limit the height to say 3/4 of the height of the window.

How can i do this?

+6
data-binding wpf
source share
2 answers

Another approach (without a converter) is to simply put it in a grid with the size of a star. Of course, this imposes some restrictions on your layout. So it depends on other content whether this approach can be used or not.

<Grid> <Grid.RowDefinitions> <RowDefinition Height="0.75*"/> <RowDefinition Height="0.25*"/> </Grid.RowDefinitions> <ListView Grid.Row="0" VerticalAlignment="Top"/> <!-- some other content --> </Grid> 

Since you wanted to specify MaxHeight from a ListView, I set VerticalAlignment to Top so that it would not use all available space if it is not required. Of course, you can also set this value to Bottom or Stretch , depending on your requirements.

+3
source share

You can use the converter to calculate the height depending on the height of the window, something like this ...

You need to pass Window.ActualHeight to the converter - it will return the window height multiplied by 0.75. If, for some reason, when the converter hit, Window.ActualHeight is NULL (or you accidentally passed something that cannot be converted to double), it will return double.NaN, which will set the height to Auto.

 public class ControlHeightConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { double height = value as double; if(value != null) { return value * 0.75; } else { return double.NaN; } } } 

Bind this to your management so ... (obviously this is a very shortened version of haml!)

 <Window x:Name="MyWindow" xmlns:converters="clr-namespace:NamespaceWhereConvertersAreHeld"> <Window.Resources> <ResourceDictionary> <converters:ControlHeightConverter x:Key="ControlHeightConverter"/> </ResourceDictionary> </Window.Resources> <ListView MaxHeight="{Binding ElementName=MyWindow, Path=ActualHeight, Converter={StaticResource ControlHeightConverter}}"/> </Window> 
+1
source share

All Articles