首页 > 解决方案 > Why is my DataGrid showing up in the wrong Grid column?

问题描述

My DataGrid should be showing up in the middle of three Grid columns, Column 1, but instead it appears to be showing up in Column 0, as seen here:

DataGrid in Column 0 instead of Column 1

You can see there that my Cancel button (Column 0) and my OK button (column 2) are right next to each other because the DataGrid appears to be occupying Column 0, instead of Column 1. However, when I break my code during execution and do Grid.GetColumn(DataGridFilteredFixesReportsTable), it properly returns 1.

I've also tried moving the DataGrid to Column 2 to see what happens, but the visual result was exactly the same.

What am I doing wrong or misunderstanding about how this works?

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <Grid Margin="20,20,20,20" Name="SettingsGrid" Visibility="Visible">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MaxHeight="600"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="100"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <DataGrid Grid.Row="0" Grid.Column="1" 
              Name="DataGridFilteredFixesReportsTable" 
              ItemsSource="{Binding Path=FilteredFixesReportsTable.DefaultView}"
              DataContext="{Binding ElementName=SeeAllFixesReportsWindow}"
              AutoGenerateColumns="True"
              FontSize="28" FontFamily="Times New Roman"
              IsReadOnly="True">
            </DataGrid>
        </ScrollViewer>
        <Button Grid.Row="2" Grid.Column="0" 
            Name="ButtonCancel" 
            Click="OkCancel_Click" 
            Content="Cancel" 
            FontSize="32" FontFamily="Times New Roman" FontWeight="Bold" Background="Pink"/>
        <Button Grid.Row="2" Grid.Column="2" 
            Name="ButtonOk" 
            IsDefault="True" 
            Click="OkCancel_Click" 
            Content="OK" 
            FontSize="32" FontFamily="Times New Roman" FontWeight="Bold" Background="PaleGreen"/>
    </Grid>
</ScrollViewer>

标签: c#wpf

解决方案


Direct children of the Grid should have the column and row specifications. Your DataGrid is a child of ScrollViewer, not Grid so it is ignoring those properties.

Try:

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <Grid Margin="20,20,20,20" Name="SettingsGrid" Visibility="Visible">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MaxHeight="600"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="100"/>
        </Grid.ColumnDefinitions>
        <!-- Should be set here -->
        <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="0" Grid.Column="1" HorizontalScrollBarVisibility="Auto">
            <DataGrid 
              Name="DataGridFilteredFixesReportsTable" 
              ItemsSource="{Binding Path=FilteredFixesReportsTable.DefaultView}"
              DataContext="{Binding ElementName=SeeAllFixesReportsWindow}"
              AutoGenerateColumns="True"
              FontSize="28" FontFamily="Times New Roman"
              IsReadOnly="True">
            </DataGrid>
        </ScrollViewer>
        <Button Grid.Row="2" Grid.Column="0" 
            Name="ButtonCancel" 
            Click="OkCancel_Click" 
            Content="Cancel" 
            FontSize="32" FontFamily="Times New Roman" FontWeight="Bold" Background="Pink"/>
        <Button Grid.Row="2" Grid.Column="2" 
            Name="ButtonOk" 
            IsDefault="True" 
            Click="OkCancel_Click" 
            Content="OK" 
            FontSize="32" FontFamily="Times New Roman" FontWeight="Bold" Background="PaleGreen"/>
    </Grid>
</ScrollViewer>

Since it isn't set on the ScrollViewer it is defaulting to 0 and 0 for row and column.


推荐阅读