Search code examples
wpfdatagriditemscontrol

WPF - Set initial size of DataGrid within ItemsControl


I Have multiple DataTable's shown via DataGrid:

    <ScrollViewer VerticalScrollBarVisibility="Visible">
        <ItemsControl ItemsSource="{Binding Path = Data}" ScrollViewer.VerticalScrollBarVisibility="Visible">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Height="auto" >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <DataGrid Height="auto"   VerticalAlignment="Stretch"  HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" ItemsSource="{Binding}">
                        </DataGrid>
                        <GridSplitter Grid.Row="0" Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" />
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>

Is there an option to limit the initial size to parent's height, or a part of him (for example 50%), and make it resizable (height) by user ?


Solution

  • You are binding Data property to your ItemsControl. The same can go to the Height. Create public property and bind it to Height in view.

    <RowDefinition Height="{Binding MyHeightProperty}" />