简体   繁体   English

在WPF数据网格中突出显示单元格时看不到任何文本

[英]No text is visible when highlighting a cell in WPF datagrid

I am writing a quick and dirty WPF application as a complete WPF novice. 我正在编写一个快速而肮脏的WPF应用程序,作为一个完整的WPF新手。 I have a simple datagrid: 我有一个简单的数据网格:

<DataGrid ItemsSource="{Binding Path=BetterFoods}" Grid.Row="1" Grid.Column="0" AutoGenerateColumns="True" Loaded="DataGrid_Loaded">
    <DataGrid.Resources>
        <local:ValueColorConverter x:Key="colorconverter"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF0033"/>
    </DataGrid.Resources>
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource colorconverter}}"/>
        </Style>
   </DataGrid.CellStyle>
</DataGrid>

The datagrid is coloured by a simple IValueConverter , which is virtually identical to loads of examples from tutorials and Stack Overflow: 数据网格由一个简单的IValueConverter着色,实际上与教程和Stack Overflow中加载的示例相同:

class ValueColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return Brushes.Beige;
        }
        else if (value is string)
        {
            string str = ((string)value).Trim();
            if (str.Equals(string.Empty))
            {
                return Brushes.Beige;
            }
            else if (str.Equals("0"))
            {
                return Brushes.LightYellow;
            }
        }

        return System.Windows.DependencyProperty.UnsetValue;
    }

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

The IValueConverter works exactly like it should, but it introduces a strange side effect: selecting a row makes all values in its cells disappear . IValueConverter工作原理与应该的完全相同,但是它带来了一个奇怪的副作用:选择一行会使其单元格中的所有值消失 The values are still there, since changing selection or double-clicking a cell makes them visible again (see .gif below). 这些值仍然存在,因为更改选择或双击单元格会使它们再次可见(请参见下面的.gif)。

GifCam记录的有问题的行为的屏幕截图

This is obviously unfortunate, since one usually highlights a row to have a closer look at its data. 这显然是不幸的,因为通常会突出显示一行以更仔细地查看其数据。

What is causing this behaviour and how do I fix it? 是什么引起了这种现象,我该如何解决?

Add a resource (SystemColors.HighlightTextBrushKey) that changes the text brush to something darker so you can actually see the text: 添加资源(SystemColors.HighlightTextBrushKey),将文本笔刷更改为较暗的颜色,以便您可以实际看到文本:

<DataGrid ItemsSource="{Binding Path=BetterFoods}" Grid.Row="1" Grid.Column="0" AutoGenerateColumns="True">
    <DataGrid.Resources>
        <local:ValueColorConverter x:Key="colorconverter"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF0033"/>
        <!-- ADDED: -->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
    </DataGrid.Resources>
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource colorconverter}}"/>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>

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

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