繁体   English   中英

使用数据绑定对彩色DataGrid进行排序

[英]Sorting a with databinding colorized datagrid

我最近尝试使用以下代码(XAML)为datagrid某些行着色:

<Window.Resources>
    <local:IsDateExpiredConverter x:Key="IsDateExpiredConverter" />
    <Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Notenmeldung, Converter={StaticResource IsDateExpiredConverter}}" Value="True">
                <Setter Property="Background" Value="#d2efbd"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

方法:

[ValueConversion(typeof(DateTime), typeof(bool))]
public class IsDateExpiredConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (DBNull.Value == value)
            return false;

        DateTime date = (DateTime)value;
        DateTime curDate = DateTime.Now;


        if (date < curDate)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

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

因此,它所做的只是给DataGridRow着色,其中“ Notenmeldung ”小于当前日期。 这样很好。

问题

当我单击标题以对网格进行排序时,有时绿色的行会消失。 这取决于我要排序的列以及我要排序的方向。 例如,我在两个方向上对一列进行排序,一切都很好。 当我在一个特定方向上排序时(另一方向它又变绿),另一只显示颜色(行仍在)。 然后,我们也遇到了什么都没有绿色的情况。 DataGrid的来源是DataBaseTable ,我看不到数据类型和单元格内容之间的任何规律性。


我非常感谢每一个小提示。

解:

不要使用AlternatingRowBackground ,这会覆盖更改的颜色。

暂无
暂无

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

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