WPF Datagrid的选定行有奇怪的行为

时间:2015-08-27 13:19:44

标签: c# wpf datagrid row

我有WPF datagrid:

<DataGrid IsReadOnly="True" x:Name="PivotDataGridView" Grid.Row="1" Grid.ColumnSpan="3" Margin="5,30,10,20" CanUserSortColumns="False" CellStyle="{StaticResource PivotCellStyle}" Grid.Column="1">

当我选择某行时,它看起来像这样:

http://i.stack.imgur.com/36d4X.png

有人知道如何解决它吗?

修改

这是用过的风格:

<Style x:Key="PivotCellStyle" TargetType="DataGridCell">
    <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource DifferenceToTotalBrushConverter}}" />
</Style>

这是转换器:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGridCell currentCell;

        try
        {
            var context = (value as DataGridCell).DataContext;
            var Prices = (context as PivotDataGridViewItem).Price;
            currentCell = value as DataGridCell;

            if (currentCell.Column.DisplayIndex > 3 && currentCell.Column.DisplayIndex % 2 == 0)
            {
                int index = GetIndexPrices(currentCell);

                if(index < Prices.Count)
                {
                    decimal currentPrice = Prices[index];
                    decimal totalPrice = Prices[0];

                    if (currentPrice != 0)
                    {
                        if (Math.Abs(currentPrice - totalPrice) < 0.20m && currentPrice != totalPrice)
                        {
                            return MainWindow.PivotCentPriceColor;
                        }
                        else if (currentPrice < totalPrice)
                        {
                            return MainWindow.PivotLowerPriceColor;
                        }
                        else if (currentPrice > totalPrice)
                        {
                            return MainWindow.PivotHigherPriceColor;
                        }
                    }
                }
            }
        }
        catch (InvalidCastException)
        {
            return DependencyProperty.UnsetValue;
        }
        catch (NullReferenceException)
        {
            return DependencyProperty.UnsetValue;
        }
        catch (FormatException)
        {

        }

        return DependencyProperty.UnsetValue;
    }

没有实现ConvertBack会出现问题吗?

1 个答案:

答案 0 :(得分:0)

我发现转换器以某种方式修改突出显示的行样式。将backgroundcolor修改为白色,但也保持字体颜色为白色。所以文字看起来像隐藏。

这有助于我:

<DataGrid.Resources>
     <Style TargetType="{x:Type DataGridRow}">
           <Style.Resources>
                 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" />
                 <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightBlue" />
                 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
                 <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
           </Style.Resources>
      </Style>
</DataGrid.Resources>