繁体   English   中英

WPF 日历 - 重点承诺日期

[英]WPF Calendar - Highlights Dates of Commitments

我正在尝试用“LigthPink”颜色突出显示已安排约会的重要日期的日期。 在我的 WPF MVVM 项目中,我创建了一个代码,但我无法更新日期。

我到达了以下代码:

 class ConverterHigligthdate: IValueConverter
{
    static BindableCollection<DateTime> dict = new BindableCollection<DateTime>();

    public event PropertyChangedEventHandler PropertyChanged;

    static ConverterHigligthdate()
    {
        dict.Add(DateTime.Today);
        dict.Add(DateTime.Today.AddDays(2));
        dict.Add(DateTime.Today.AddDays(-10));
        dict.Add(DateTime.Today.AddDays(-20));
        dict.Add(DateTime.Today.AddDays(-15));
    }
    public static void AddDate(DateTime date)
    {
        dict.Add(date);
    }
    public static void RemoveDate(DateTime date)
    {
        dict.Remove(date);
    }
    public void Clear()
    {
        dict.Clear();
        dict.Refresh();
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string text = null;
        if (dict.Contains((DateTime)value))
            text = null;
        else
            text = "";

        return text;
    }

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

}

在视图中:

 <Window.Resources>
    <local:ConverterHigligthdate x:Key="ConverterHigligthdate"/>
    <Style x:Key="calendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}">
        <Setter Property="Margin" Value="8"/>
        <Setter Property="FontSize" Value="13"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Converter={StaticResource ConverterHigligthdate}}" Value="{x:Null}">
                <Setter Property="Background" Value="LightPink"/>                    
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid Margin="5">
        <Calendar SelectionMode="MultipleRange" CalendarDayButtonStyle="{DynamicResource calendarDayButtonStyle}"/>       
</Grid>

结果

有谁知道如何实现使其工作的东西?

你以错误的方式解决这个问题。 使用 MVVM,您始终在视图 model 层中执行业务逻辑,而不是在您的转换器中(它们是视图层的一部分)。

有很多方法可以解决这个问题,但通常您希望您的视图 model 层以视图可以轻松使用的格式准备数据。 出于性能的目的,让我们将您选择的所有日期包装在一个查找表中:

public class MainViewModel
{
    public HashSet<DateTime> Dates { get; } = new HashSet<DateTime>();

    public MainViewModel()
    {
        // highlight today and tomorrow
        this.Dates.Add(DateTime.Today);
        this.Dates.Add(DateTime.Today.AddDays(1));
    }
}

现在,您要在 CalendarDayButtonStyle 中添加一个 DataTrigger。 当相关按钮的日期在您的收藏中时,您就想更改背景颜色:

    <Style x:Key="CalendarDayButtonStyle" TargetType="CalendarDayButton">
        <Style.Triggers>
            <DataTrigger Value="True">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource LookupConverter}">
                        <Binding />
                        <Binding Path="DataContext.Dates" RelativeSource="{RelativeSource AncestorType=Calendar}" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="Pink" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

因此,您现在只需要一个转换器来进行查找。 我们需要传入查找表以及要查找的值,因此我们可以使用 MultiBinding。 如果我们真的想的话,这实际上是可以放在视图 model 中的逻辑,但它不引用任何视图模型特定的数据,并且可以在其他地方重用,所以我们将规则弯曲一点点:

public class LookupConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var date = (DateTime)values[0];
        var dates = values[1] as HashSet<DateTime>;
        return dates.Contains(date);
    }

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

这就是你所需要的。 结果:

在此处输入图像描述

暂无
暂无

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

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