简体   繁体   English

在Grid控件中安装WPF DataGrid

[英]Fitting WPF DataGrid in a Grid control

I have a DataGrid control inside a Grid control in one of my WPF windows. 我在一个WPF窗口中的Grid控件中有一个DataGrid控件。

<Grid>
  <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
  </Grid.RowDefinitions>
  <SomeControl Grid.Row="0" />
  <DataGrid Grid.Row="1" VerticalScrollBarVisibility="Visible" VerticalAlignment="Stretch"/>
</Grid>

The problem is that when I add rows to the DataGrid it flows out of the containing window and its scroll bar remains inactive. 问题是,当我向DataGrid添加行时,它会从包含窗口流出,并且其滚动条保持不活动状态。 How do I solve this problem and make the DataGrid's scroll bar to function correctly? 如何解决此问题并使DataGrid的滚动条正常工作?

You may try as follows 您可以尝试如下

  <Grid>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="200" />
            </Grid.RowDefinitions>
            <DataGrid   Grid.Row="1" HorizontalAlignment="Left" Margin="54,65,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="382" VerticalScrollBarVisibility="Visible">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="ID"/>
                    <DataGridTextColumn Header="ViewCount" />
                    <DataGridTextColumn Header="Title" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>

You need to provide some height to the DataGrid ,as you have RowDefinition Height="*" so the vertical Scrolbar was not active,try to give some height to the DataGrid. 你需要为DataGrid提供一些高度,因为你有RowDefinition Height =“*”所以垂直Scrolbar没有活动,试着给DataGrid一些高度。 Hope it will help you 希望它会对你有所帮助

I had the same problem;my datagrid would flow out of my grid. 我有同样的问题;我的数据网格将流出我的网格。 I have a bunch of grids I'm using, so the app will scale to the screen size. 我有一堆我正在使用的网格,因此应用程序将扩展到屏幕大小。 The lower row of the main grid has three data grids. 主网格的下排有三个数据网格。 Path=ActualHeight will only work when you are fitting the datagrid with margins; Path = ActualHeight仅在您使用边距拟合数据网格时才有效; take it out! 把它拿出来! Now with my app everything resizes with the screen, except my data input UI elements, which I don't want them to. 现在使用我的应用程序,除了我不想要的数据输入UI元素外,所有内容都会随屏幕调整大小。 Good luck! 祝好运! I hope this helps. 我希望这有帮助。

<Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Border Grid.Column="0" Margin="3" >
                <DataGrid x:Name="DgDbNames" CanUserAddRows="false" Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}" VerticalScrollBarVisibility="Auto" AlternatingRowBackground="LightGray"  ItemsSource="{Binding Source=Dbs}" AutoGenerateColumns="False" CanUserResizeColumns="True" Margin="10"  />
            </Border>
            <Border Grid.Column="1" Margin="3" >
                <DataGrid x:Name="DgTableNames" CanUserAddRows="false" Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}" VerticalScrollBarVisibility="Auto" AlternatingRowBackground="LightGray"  ItemsSource="{Binding Source=Tables}" AutoGenerateColumns="False" CanUserResizeColumns="True" Margin="10"  />
            </Border>
            <Border Grid.Column="2" Margin="3" >
                <DataGrid x:Name="DgSprocNames" CanUserAddRows="false" Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}" VerticalScrollBarVisibility="Auto" AlternatingRowBackground="LightGray"  ItemsSource="{Binding Source=Sprocs}" AutoGenerateColumns="False" CanUserResizeColumns="True" Margin="10"  />
            </Border>
        </Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM