1

I have a data grid:

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

And a converter:

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();
    }
}

ToolTip, that opens when cell content is trimmed, is in DataGrid.Resources part, I get it from here. It works, but now, when I click any cell it looks like this:

image

When I am selecting a cell or cells, all values in it disappear and cell is not highlighted... Items source of my DataGrid is DataTable, if it does matter. How can I fix this issue?

Community
  • 1
  • 1

1 Answers1

1

It's interesting! Just setting the DataGrid.CellStyle does reproduce the issue.

I think, that the problem is, that the Background color in ControlTemplate is white, so you don't see the selection.

Add Background="{TemplateBinding Background}" to the TextBlock.

Rekshino
  • 6,954
  • 2
  • 19
  • 44
  • Now i see BlueViolet text in cell, when select it. But cell not highlighted. All that happens - text becomes BlueViolet... – Сиволапый Jan 10 '20 at 14:26
  • See update answer and remove `HighlightTextBrushKey` from the `DataGrid.Resources`. – Rekshino Jan 10 '20 at 14:51
  • Thanks a lot, it works! Now only borders of selected cell not highlighted (don't get thicker), this is not critical, but can it be fixed? – Сиволапый Jan 10 '20 at 15:06
  • Yes, it's possible if you surround the `TextBlock` with `Border` and apply the `TemplateBinding` for `BorderThickness` and `BorderBrush`. – Rekshino Jan 10 '20 at 15:12