【问题标题】:WPF Datagrid's selected row has strange behaviourWPF Datagrid 的选定行有奇怪的行为
【发布时间】:2015-08-27 13:19:43
【问题描述】:

我有 WPF 数据网格:

<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 会不会有问题?

【问题讨论】:

  • 我想这取决于您的 DataGrid 的样式,或者可能取决于 Cell 的样式。你应该发布一些关于它的额外信息......

标签: c# wpf datagrid row


【解决方案1】:

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

这对我有帮助:

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

【讨论】:

    猜你喜欢
    • 2011-05-17
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2011-10-10
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    相关资源
    最近更新 更多