【发布时间】:2014-02-08 15:12:42
【问题描述】:
我有一个包含 3 列的 DataGrid,其中 2 列是 DataGridTextColumns,最后一个是包含 CheckBox 的 DataGridTemplateColumn。它绑定到 Field 对象的 ObservableCollection。
用户可以根据 ComboBox 的 SelectedItem 更改 DataGrid 中的数据,该组合框出现在 DataGrid 的正上方。此 ComboBox 绑定到 RecordType 对象列表。我选择了一个 List,因为其中的值不会改变。
我遇到的问题很奇怪。有时,但并非总是如此(错误是非常情绪化的),当您更改 ComboBox 中的值时,当您向下滚动列表时,DataGrid 中的行显示奇怪(请参阅屏幕截图)。 DataGrid 中的某些行在其左侧出现一个小的灰色区域,并且该行的内容被推到右侧,从而难以读取单元格的值。当您继续向上或向下滚动时,其他单元格会以相同的方式显示,尽管问题似乎只影响最少的行数,大约最多 5 行。受影响的行并不总是相同的,并且可以随着 ComboBox 项的更改而更改。
为了确认,我已将屏幕截图中的某些字段名称删除,以保护我们客户的隐私。
谁能提出可能导致此显示问题的原因?下面给出了我用于 ComboBox 和 DataGrid 的 XAML 代码。
任何建议/cmets/帮助将不胜感激。
非常感谢!
<ComboBox ItemsSource="{Binding RecordTypes}" SelectedItem="{Binding SelectedRecordType}" DisplayMemberPath="DisplayName" HorizontalAlignment="Left" Margin="9,71,0,0" VerticalAlignment="Top" Width="195"/>
<DataGrid SelectedItem="{Binding SelectedField}" ItemsSource="{Binding Fields}" GridLinesVisibility="None" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="10,98,0,0" VerticalAlignment="Top" Height="385" Width="400" IsReadOnly="True" CanUserReorderColumns="False" CanUserResizeRows="False" HeadersVisibility="Column" SelectionMode="Single" MaxColumnWidth="300">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="ToolTip" Value="{Binding Description}" />
<Style.Triggers>
<Trigger Property="ToolTip" Value="{x:Static system:String.Empty}">
<Setter Property="ToolTipService.IsEnabled" Value="False" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding DisplayName}" Header="Field" Width="150" />
<DataGridTextColumn Binding="{Binding DisplayPath}" Header="Path" Width="*" />
<DataGridTemplateColumn MinWidth="25" MaxWidth="25">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="5" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
【问题讨论】:
-
我认为这与您的工具提示有关。这也可以解释为什么它不会一直发生。
-
感谢elgonzo,我认为这已经解决了问题!但是,我认为左侧列现在完全是灰色的,因为我显然已经删除了 HeadersVisibility="Column"。如果我同时使用 RowHeaderWidth = 0 和 HeadersVisibility="Column" 那么问题仍然存在。知道如何在保持 RowHeaderWidth = 0 的同时删除左侧列吗?非常感谢您的帮助!
-
@elgonzo,您能否将您的评论添加到答案中,以便此问题可以是marked as answered?如果您可以添加一些额外的文本,以使答案看起来不像评论,我们也将不胜感激。
-
@user3124134,无法从您的示例代码中推断出为什么在设置 RowHeaderWidth="0" 时会看到一个完整的灰色列。您确定它在 DataGrid 控件内,而不是导致该效果的其他 UIElement 吗?如果它确实在 DataGrid 中,则需要调试问题。特别关注与 DataGrid 或其任何子/子元素的样式、模板和代码隐藏修改相关的任何内容。
标签: c# wpf xaml datagrid combobox