简体   繁体   English

WPF:DataGrid显示错误

[英]WPF: DataGrid Display Error

I have a DataGrid with 3 columns, 2 of which are DataGridTextColumns and the last is a DataGridTemplateColumn that contains a CheckBox. 我有一个包含3列的DataGrid,其中2列是DataGridTextColumns,最后一个是包含CheckBox的DataGridTemplateColumn。 It is bound to an ObservableCollection of Field objects. 它绑定到一个ObservableCollection的Field对象。

The user can change the data in the DataGrid based on the SelectedItem of a ComboBox, which appears just above the DataGrid. 用户可以根据ComboBox的SelectedItem更改DataGrid中的数据,ComboBox位于DataGrid的正上方。 This ComboBox is bound to a List of RecordType objects. 此ComboBox绑定到RecordType对象的List。 I chose a List as the values in it do not change. 我选择了一个List,因为它中的值不会改变。

The problem I'm having is strange. 我遇到的问题很奇怪。 Sometimes, but not always (the error is very temperamental), when changing the value in the ComboBox, the rows in the DataGrid display oddly (please see the screenshot) as you scroll down the list. 有时,但并非总是(错误是非常不稳定的),当更改ComboBox中的值时,DataGrid中的行显示奇怪(请参见屏幕截图),同时向下滚动列表。 Certain rows in the DataGrid appear with a small grey area to the left hand side of them and the row's content is pushed to the right, making it difficult to read the cells' values. DataGrid中的某些行在其左侧显示一个小灰色区域,并且该行的内容被推向右侧,这使得难以读取单元格的值。 As you continue to scroll up or down, so other cells display in the same way, though the problem only seems to affect a minimal number of rows, 5 at max roughly. 当您继续向上或向下滚动时,其他单元格以相同的方式显示,但问题似乎只影响最小行数,最大值为5。 The rows affected are not always the same and can change as the ComboBox item is changed. 受影响的行并不总是相同,并且可以随着ComboBox项目的更改而更改。

To confirm, I have blanked out certain Field names in the screenshot to protect our client's privacy. 为了确认,我在屏幕截图中删除了某些字段名称以保护我们客户的隐私。

Could anyone suggest what might be causing this display issue? 任何人都可以建议可能导致此显示问题的原因吗? The XAML code I'm using for the ComboBox and DataGrid is given below. 下面给出了我用于ComboBox和DataGrid的XAML代码。

Any suggestions/comments/help would be greatly appreciated. 任何建议/意见/帮助将不胜感激。

Many thanks! 非常感谢!

<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>

Try setting the RowHeaderWidth parameter for your datagrid to 0 , like: 尝试将datagrid的RowHeaderWidth参数设置为0 ,如:

<DataGrid
    RowHeaderWidth="0"
    ... your other parameters for the datagrid ...
>

(It appears to be the same problem as asked here: WPF DataGrid Row Header Visibility Error .) (它似乎与此处提出的问题相同: WPF DataGrid行标题可见性错误 。)

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

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