c# - 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:
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>
解决方案
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.
推荐阅读
- chart.js - Chart.js:在日志 x 轴上创建自定义主要刻度?
- javascript - jQuery nextUntil 更高级别
- reactjs - 找不到模块:无法解析“bootstrap/less/bootstrap.less”
- docker - 如何获取构建输出状态作为 docker 命令退出状态
- excel - 检查单元格是否包含特定的子字符串
- c - 如何修复我的自定义 strlcat 以 SIGABRT 终止?
- c# - 我正在尝试在第二行为 comboBox 添加不同的项目
- pandas - Pandas 基于 sessionid 随机拆分/选择数据集
- https - 无法设置 webrick https 服务器 + sinatra + opalrb(sprockets)
- python - 在 Python 中重复执行函数的最佳方法是什么?