选择确实隐藏了 DataGridCell

Selection does hide the DataGridCell

我有一个数据网格:

<DataGrid x:Name="grid1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding}" IsReadOnly="True"
              Loaded="grid1_Loaded" AutoGeneratingColumn="grid1_AutoGeneratingColumn" SelectionUnit="Cell" MouseMove="Grid1_MouseMove" LoadingRow="grid1_LoadingRow" MouseLeave="grid1_MouseLeave">

                    <DataGrid.Resources>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                                        <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" TextTrimming="CharacterEllipsis">
                                            <TextBlock.ToolTip>
                                                <ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource trimmedVisibilityConverter}}">
                                                    <ToolTip.Content>
                                                        <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}"/>
                                                    </ToolTip.Content>
                                                </ToolTip>
                                            </TextBlock.ToolTip>
                                        </TextBlock>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGrid.Resources>

                    <DataGrid.RowStyle>
                        <Style TargetType="{x:Type DataGridRow}">
                            <Setter Property="Foreground" Value="Red"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Расторжение}" Value="{x:Null}">
                                    <Setter Property="Foreground" Value="Black"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DataGrid.RowStyle>
                </DataGrid>

还有一个转换器:

public class TrimmedTextBlockVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return Visibility.Collapsed;

        FrameworkElement textBlock = (FrameworkElement)value;

        textBlock.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));

        if (((FrameworkElement)value).ActualWidth < ((FrameworkElement)value).DesiredSize.Width)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;//Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

工具提示,在裁剪单元格内容时打开,在 DataGrid.Resources 部分,我从 here 中获取它。它有效,但现在,当我单击任何单元格时,它看起来像这样:

当我选择一个或多个单元格时,其中的所有值都会消失并且单元格不会突出显示...我的 DataGrid 的项目来源是 DataTable,如果它确实重要的话。 我该如何解决这个问题?

很有趣!只需设置 DataGrid.CellStyle 即可重现该问题。

我认为,问题是 ControlTemplate 中的 Background 颜色是白色,所以您看不到选择。

Background="{TemplateBinding Background}" 添加到 TextBlock