Dynamically Changing MaxHeight based on screen resize

Jul 30, 2009 at 6:54 PM

I have scrollviewer at my Window/UserControl is set to Auto.  My screen has three sections.  the "top" section and "bottom" section are static in height when expanded.  I want my data grid to be able to grow to fix the whitespace.  

I also want to set a maxheight (dynamically?) so that the data grid itself will not grow past the whitespace   

The the datagrid scrollbars will never appear unless I set the max height.  The challenge is that my data grid is dynamic and the rest of my screen is not.


How can I set the datagrid's maxheight dynamically?

Here is the Psuedocode:


<Grid x:Name="gridCenterMain" >
		<StackPanel DockPanel.Dock="Top">
			<Expander >
				<Canvas Height="85">  <<--STATIC HEIGHT
					//Filter controls

			<Expander >
				<Canvas  Height="Auto">
					<DataGrid Canvas.Left="24" Canvas.Top="12"/>  <<<<----THIS NEEDS TO HAVE MAX HEIGHT SET DYNAMICALLY SO SCROLLBARS APPEAR

 		<Canvas  DockPanel.Dock="Bottom">
			//bottom buttons      <<--STATIC HEIGHT
</Canvas> </DockPanel> </Grid> </ScrollViewer>






Sep 19, 2009 at 12:37 AM

Hi Tracy, we discussed this on a different thread.  I'm going to paste the response here for others to benefit from.

My recommendation here would be to set a layout panel (such as Grid or StackPanel) in the middle section (where DataGrid will go) which will expand to fill the available space, name that panel (so you can bind to it easily), and then bind DataGrid’s Height (and MaxHeight if you find you need to) to the Height of that panel.  Here’s some sample code:

<Window x:Class="WpfApplication2.MainWindow"



        Title="MainWindow" Height="500" Width="500">


        <Grid x:Name="gridCenterMain" >


                <RowDefinition Height="Auto"/>

                <RowDefinition Height="*"/>

                <RowDefinition Height="Auto"/>



                    <Expander Grid.Row="0" IsExpanded="True">

                        <Canvas Height="85">

                            <!--STATIC HEIGHT

                                      //Filter controls-->




                            <Grid Grid.Row="1" x:Name="MiddleCanvas"  Height="Auto">

                                <DataGrid Canvas.Left="24" Canvas.Top="12" Height="{Binding ElementName=MiddleCanvas, Path=Height}" Background="BlueViolet"

                                          MaxHeight="{Binding ElementName=MiddleCanvas, Path=Height}"/>

                                        <!--THIS NEEDS TO HAVE MAX HEIGHT SET DYNAMICALLY SO SCROLLBARS APPEAR-->




            <Expander  Grid.Row="2" IsExpanded="True">


                <!--//bottom buttons STATIC HEIGHT-->

                <Button Content="Button 1" />

                <Button Content="Button 2"/>









Saied Khanahmadi