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