locked
If total minutes is less than or equal to 60 minutes then return true - IValueConverter RRS feed

  • Question

  • User-310655528 posted

    I am having trouble with my converter,  I have a DateTime column in my DataGrid which I subtract from the systems current time to get the total minutes difference and then from there if total minutes is less than or equal to 60 minutes my DataGrid Row should change colour to RED. The problem I have is every row is changing. 

    in runtime my code it seems to be subtracting the two times okay, not really sure what is happening.

    C#

    using System;
    using System.Windows.Data;
    
    namespace Project
    {
        [ValueConversion(typeof(DateTime), typeof(bool))]
        public class TimeValueConverter:IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
    
    
                DateTime end = (DateTime)value;
                DateTime curtime = DateTime.Now;
    
                TimeSpan span = curtime - end;
                var totalMinutes = span.Minutes;
    
                if (totalMinutes <= 60)
                {
                    return true;
                }
                else
                {
                    return false;
                }
    
            }
    
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    XAML

    <DataGrid.RowStyle>
                                    <Style TargetType="{x:Type DataGridRow}">
                                        <Setter Property="Height" Value="40"></Setter>
                                        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding JobDate.Date}" Value="{x:Static System:DateTime.Today}">
                                                <Setter Property="Background" Value="#ff8d00"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Path=JobDate, Converter={StaticResource ValueConverter}, ConverterParameter=1}" Value="True">
                                                <Setter Property="Background" Value="#ef2700" />
                                                <Setter Property="Foreground" Value="White" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Path=JobTimeFinish, Converter={StaticResource TimeValueConverter}, ConverterParameter=3}" Value="True">
                                                <Setter Property="Background" Value="BlueViolet" />
                                                <Setter Property="Foreground" Value="White" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </DataGrid.RowStyle>
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
                                   
                                    <DataGridTextColumn Header="Date" Binding="{Binding Path=JobDate, StringFormat=\{0:dd/MM/yyyy\}}" x:Name="JobDate"/>
                                    <DataGridTextColumn Header="Start Time" Binding="{Binding JobTimeStart, StringFormat={}{0:HH:mm}}" x:Name="JobTimeStart"/>
                                    <DataGridTextColumn Header="Finish Time" Binding="{Binding JobTimeFinish, StringFormat={}{0:HH:mm}}" x:Name="JobTimeFinish"/>
     
                                </DataGrid.Columns>

    Friday, December 20, 2019 9:53 AM

All replies

  • User576623942 posted

    instead of span.Minutes, use span.TotalMinutes. you will get the total minutes difference instead of only minute part you were getting using span.Minutes

    Monday, December 23, 2019 6:28 AM