WPF DataGrid for Vertical Calibration

I want to place a DataGrid inside a HeaderedContentControl, but the DataGrid does not get a vertical scroll bar. It seems to be sized to hold all the lines at once, and the bottom part disappears from view.

If I put the same DataGrid in the Border element, I will get the behavior that I want.

I brought it to this minimal example:

<Grid> <HeaderedContentControl Margin="10,10,10,161" > <HeaderedContentControl.Header >test</HeaderedContentControl.Header> <!-- I want it Here but then no Vertical Scroll--> <DataGrid ItemsSource="{Binding Path=AllData}" AutoGenerateColumns="True" /> </HeaderedContentControl> <Border Margin="10,169,10,10"> <!--Here it does scroll --> <DataGrid ItemsSource="{Binding Path=AllData}" AutoGenerateColumns="True" /> </Border> </Grid> 

A few notes:

  • I could not get it to work using HeaderedContentControl.VerticalContentAlignment
  • this problem is related to this question , but I think I expanded it a bit and that there is a better answer.
  • using a ScrollViewer around a DataGrid is not a solution because it scrolls the title out of sight.
  • I am using WPF4
+4
source share
1 answer

You see this behavior because the default template for HeaderedContentControl uses a StackPanel to display its contents. Because the StackPanel takes on the size of its children, the DataGrid expands its height so that each element appears on the screen without scroll bars. The display is then cropped due to the size of the HeaderedContentControl .

Changing the template to use Grid or DockPanel solves this problem:

 <Style TargetType="{x:Type HeaderedContentControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type HeaderedContentControl}"> <DockPanel> <ContentPresenter DockPanel.Dock="Top" ContentSource="Header" /> <ContentPresenter /> </DockPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> 
+6
source

Source: https://habr.com/ru/post/1313276/


All Articles