简体   繁体   English

ItemsControl中的GroupStyle HeaderTemplate无法正确更新

[英]GroupStyle HeaderTemplate in an ItemsControl does not update correctly

I am making a layout for a message inbox, and am currently working on separating messages automatically based on days/weeks/months/years. 我正在为邮件收件箱设计布局,目前正在根据天/周/月/年自动分离邮件。 More specifically I have a header that says "Today" for all messages recieved on the current date, "Yesterday" for yesterday, "3+ days ago" for anything from 3 to 6 days ago. 更具体地说,我有一个标头,其中显示当前日期收到的所有消息的“今天”,昨天的“昨天”,“ 3天以上”的3到6天前的消息。 "Last week" for anything 7-13 days ago to name some examples. 列举7-13天前的任何内容的“上周”。 You get the idea. 你明白了。

All of this works good so far, except one thing. 到目前为止,所有这些工作都很好,除了一件事。 If I leave the application on overnight my messages from today will be labelled as "Today", yet all older headers will not change. 如果我将应用程序放在夜间,那么我今天发送的消息将被标记为“今天”,但是所有较旧的标头都不会更改。 So yesterday is also labelled "Today", 2 days ago is "Yesterday" and so on. 因此昨天也标记为“今天”,而2天前也标记为“昨天”,依此类推。 They are still grouped as they should, it's just the heading that won't update. 它们仍按应有的方式分组,只是标题不会更新。 It feels like it's missing some kind of OnPropertyChanged functionality, but how would that work in it's current state? 感觉好像缺少了某种OnPropertyChanged功能,但是在当前状态下该如何工作?

How my GroupStyle is set up: 我的GroupStyle的设置方式:

<ItemsControl>
  <ItemsControl.Resources>
    <CollectionViewSource x:Key="MessageList" Source="{Binding Messages}">
      <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="MessageDate" />
      </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
  </ItemsControl.Resources>
  <ItemsControl.ItemsSource>
    <Binding Source="{StaticResource MessageList}"/>
  </ItemsControl.ItemsSource>
  <ItemsControl.GroupStyle>
   <GroupStyle>
     <GroupStyle.HeaderTemplate>
       <DataTemplate>
         <StackPanel Margin="0 0 0 15">
           <TextBlock Text="{Binding Path=Items[0].MessageDate, Converter={StaticResource DateTimeToStringConverter}}"/>
           <Path Data="m 0 0 100 0"/>
          </StackPanel>
        </DataTemplate>
      </GroupStyle.HeaderTemplate>
    </GroupStyle>
  </ItemsControl.GroupStyle>
</ItemsControl>

My Converter (to change from DateTime to a string to present) 我的转换器(从DateTime更改为要显示的字符串)

public class DateTimeToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((DateTime)value == DateTime.Now.Date)
        {
            return "Today";
        }
        else if ((DateTime)value == DateTime.Now.AddDays(-1).Date)
        {
            return "Yesterday";
        }
        else if ((DateTime)value == DateTime.Now.AddDays(-2).Date)
        {
            return "2 Days Ago";
        }
        return "3+ Days Ago";
    }

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

Can also add that it still works if I restart the application, but that I can't leave it on overnight without it malfunctioning is too annoying to overlook. 还可以补充一点,如果我重新启动应用程序,它仍然可以工作,但是如果它无法正常运行,我不能整夜将其打开,这太令人讨厌了,无法忽略。

More context if needed from my older question 如果我的较老问题需要更多的背景信息

You need to raise the PropertyChanged event for the databound MessageDate property by midnight each day if you want this to work. 如果要使其工作,则需要每天在午夜之前引发数据绑定的MessageDate属性的PropertyChanged事件。 The converter won't be invoked again until this event is raised. 引发此事件之前,不会再次调用该转换器。

You could use a task scheduling framework such as Quartz.NET or FluentScheduler to run the code that raises the event in your view model at a certain time. 您可以使用Quartz.NETFluentScheduler等任务调度框架来运行在特定时间在视图模型中引发事件的代码。

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

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