1
votes

I have the following structure in my WPF application. (Using Prism and Regions)

Window----> UserControl----> DockPanel------> Grid(2X2)------>Row0Col0-- Grid (2X1)------> ItemsControl------> UserControl(injected into ItemsControl with Prism)-----> DockPanel-----> DataGrid.(60 rows, 10 columns)

The behavior that I expect is that the DataGrid will size itself to fit the size of the grid cell and display both scrollbars because it is too big to fit. But it doesnt. It remains its maximum size and cuts out of the edges of the grid cell. All cells of both grids have no size specifications (Auto Sized). When I explicitly specify datagrid's height and width, I see the scrollbars, but of course I don't want to do that.

Please help!. Thanks

I have saved the screenshots at the following link. http://s1199.photobucket.com/albums/aa467/vikasgoyalgzs/

1

1 Answers

3
votes

You say: "All cells of both grids have no size specifications (Auto Sized)" - this is where the problem is. When the grid cell is auto sized the grid gives the content in that cell as much space as it wants (doesn't matter if it fits in the window or not). To fix it you have to put your DataGrid into a star-sized cell.

<Grid>
   <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>      
   </Grid.RowDefinitions>

   <Border Grid.Row="0">
     <!-- Content that will take as much space as it wants -->
   </Border>

   <Border Grid.Row="1">
     <!-- Content that will take all the remaining space -->
   </Border>
</Grid>

UPDATE: Based on the screenshots you provided...

First, get rid of the DockPanel in the top level control. DockPanel gives its child all the space it asks for. If it is not a "fill" child (LastChildFill="True"). Use grid instead of DockPanel (i.e. at the top level a grid with two rows - one auto-sized for the menu and the second star-size for the rest of the stuff, the in that star-size row put another grid for you items controls, etc.).

Remember, whenever you put the content either in an auto-size cell in a grid or in a DockPanel with dock type different than Fill, the content will take as much space as it required without showing a scroll bar (it will go beyond the window).

UPDATE 2: Looking at the new screenshots (see comments to this post)...

OK, I think I see the problem. The thing is that ItemsControl uses StackPanel to display its children, but StackPanel also gives its children all the space they want (your DataGrid thinks that it has enough space to render itself without scroll bars).

To fix that you need to put your items controls inside an ScrollViewer like this:

<ScrollViewer VerticalScrollBarVisibility="Auto"
              HorizontalScrollBarVisibility="Auto">
    <ItemsControl ... />
</ScrollViewer>