C# 根据日期时间更改列表框项

C# 根据日期时间更改列表框项,c#,datetime,windows-phone-7,listbox,C#,Datetime,Windows Phone 7,Listbox,我有一个列表框,其中绑定了一些项目。这些项目是从如下文件中读取的: private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { bindList(); } private void bindList() { var appStorage = IsolatedStorageFi

我有一个列表框,其中绑定了一些项目。这些项目是从如下文件中读取的:

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            bindList();
        }

        private void bindList()
        {
            var appStorage = IsolatedStorageFile.GetUserStoreForApplication();

            string[] fileList = appStorage.GetFileNames();

            List<Activity> activities = new List<Activity>();


            foreach (string file in fileList)
            {

                string fileName = file;

                string cYear = file.Substring(0, 4);
                string cMonth = file.Substring(5, 2);
                string cDay = file.Substring(8, 2);
                string cHour = file.Substring(11, 2);
                string cMinute = file.Substring(14, 2);
                string cSeconds = file.Substring(17, 2);

                DateTime dateCreated = new DateTime(int.Parse(cYear), int.Parse(cMonth), int.Parse(cDay), int.Parse(cHour), int.Parse(cMinute), int.Parse(cSeconds));

                string dYear = file.Substring(20, 4);
                string dMonth = file.Substring(25, 2);
                string dDay = file.Substring(28, 2);

                DateTime dateDeadline = new DateTime(int.Parse(dYear), int.Parse(dMonth), int.Parse(dDay));

                string aTitle = file.Substring(31);
                aTitle = aTitle.Substring(0, aTitle.Length - 4);
                activities.Add(new Activity() { Title = aTitle, DateCreated = dateCreated.ToLongDateString(), Deadline = dateDeadline.ToLongDateString(), FileName = fileName });

            }

            activityListBox.ItemsSource = activities;

        }
我找到的每个指南都涉及特定的列表框项目(如更改第3项、第4项等),但它并不能解决我的问题。我想能够检查,如果截止日期是过去的当前日期,每次文件加载到应用程序,并相应地改变它


非常感谢您的帮助。

为了实现这一点,您首先需要在活动类中添加一个属性前景色。此属性将是一个getter属性,根据您的条件返回颜色(在本例中,如果当前日期大于截止日期,则返回红色或绿色)。请注意,我已将您的截止日期数据类型更改为Date,以允许对日期进行比较

public DateTime Deadline { get; set; }
public Color Forecolor
{
    get
    {
        if (DateTime.Now > Deadline)
            return Colors.Red;
        else
            return Colors.Green;
    }
}
现在将控件前景属性绑定到此属性Forecolor

Foreground="{Binding Forecolor, Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"
由于“前景”属性需要笔刷,因此它不能仅使用颜色绑定,因此需要使用将颜色转换为笔刷的转换器

在项目中定义转换器类

    public class ColorToSolidColorBrushValueConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (null == value)
        {
            return null;
        }
        // For a more sophisticated converter, check also the targetType and react accordingly..
        if (value is Color)
        {
            Color color = (Color)value;
            return new SolidColorBrush(color);
        }
        // You can support here more source types if you wish
        // For the example I throw an exception

        Type type = value.GetType();
        throw new InvalidOperationException("Unsupported type [" + type.Name + "]");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // If necessary, here you can convert back. Check if which brush it is (if its one),
        // get its Color-value and return it.

        throw new NotImplementedException();
    }
}
最后在窗口资源中定义转换器

<Window.Resources>
    <local:ColorToSolidColorBrushValueConverter  x:Key="ColorToSolidColorBrush_ValueConverter"/>
</Window.Resources>


注意:我已经输入了来自WPF项目的代码。如果您的项目在WP7中,可能会有一些语法问题(尽管我认为它应该可以工作)。不过原理是一样的。

您可以使用转换器来实现这一点

<UserControl.Resources>
    <ResourceDictionary>
        <local:DateToColorConverter x:Key="DateToColorConverter"/>
    </ResourceDictionary>
</UserControl.Resources>

...

<TextBlock Name="activityDateCreated"
    Text="{Binding DateCreated, StringFormat='Stworzono: {0}'}"                                  
    Margin="10"
    Foreground="{Binding Deadline, Converter={StaticResource DateToColorConverter}" />


...


Your Converter (put this in your code behind)...

public class DateToColorConverter : IValueConverter
{
    static SolidColorBrush _normalColor = new SolidColorBrush(Colors.Black);
    static SolidColorBrush _pastDeadlineColor = new SolidColorBrush(Colors.Red);

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is DateTime)
        {
            var deadline = value as DateTime;
            return deadline < DateTime.Now ? _pastDeadlineColor : _normalColor;
        }

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

...
...
你的转换器(把它放在你的代码后面)。。。
公共类DateToColorConverter:IValueConverter
{
静态SolidColorBrush _normalColor=新的SolidColorBrush(Colors.Black);
静态SolidColorBrush\u pastDeadlineColor=新的SolidColorBrush(Colors.Red);
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
if(值为DateTime)
{
var deadline=日期时间值;
返回截止日期
顺便说一句,您应该使用ObservableCollection而不是列表来保存活动对象。另外,确保活动对象支持INotifyPropertyChanged,并且所有属性方法都调用PropertyChanged

<UserControl.Resources>
    <ResourceDictionary>
        <local:DateToColorConverter x:Key="DateToColorConverter"/>
    </ResourceDictionary>
</UserControl.Resources>

...

<TextBlock Name="activityDateCreated"
    Text="{Binding DateCreated, StringFormat='Stworzono: {0}'}"                                  
    Margin="10"
    Foreground="{Binding Deadline, Converter={StaticResource DateToColorConverter}" />


...


Your Converter (put this in your code behind)...

public class DateToColorConverter : IValueConverter
{
    static SolidColorBrush _normalColor = new SolidColorBrush(Colors.Black);
    static SolidColorBrush _pastDeadlineColor = new SolidColorBrush(Colors.Red);

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is DateTime)
        {
            var deadline = value as DateTime;
            return deadline < DateTime.Now ? _pastDeadlineColor : _normalColor;
        }

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