Dynamically Changing MaxHeight based on screen resize

Jul 30, 2009 at 5: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:

<ScrollViewer>

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

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

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

 

Regards,

Tracy

 

 

Coordinator
Sep 18, 2009 at 11:37 PM

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"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

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

    <ScrollViewer>

        <Grid x:Name="gridCenterMain" >

            <Grid.RowDefinitions>

                <RowDefinition Height="Auto"/>

                <RowDefinition Height="*"/>

                <RowDefinition Height="Auto"/>

            </Grid.RowDefinitions>

           

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

                        <Canvas Height="85">

                            <!--STATIC HEIGHT

                                      //Filter controls-->

                        </Canvas>

                    </Expander>

                      

                            <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-->

                            </Grid>

                       

 

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

                <StackPanel>

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

                <Button Content="Button 1" />

                <Button Content="Button 2"/>

                    </StackPanel>

            </Expander>

           

        </Grid>

    </ScrollViewer>

</Window>

 

Thanks,

Saied Khanahmadi