WPF日历-重要承诺日期

问题描述 投票:0回答:1

我正在尝试使用颜色“ 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>

result

有人知道如何实现使这项工作有效的方法吗?

c# wpf mvvm calendar wpf-controls
1个回答
0
投票

您使用的是错误的方式。使用MVVM,您总是在视图模型层中进行业务逻辑,而不必在转换器中进行业务逻辑(它们是视图层的一部分)。

有许多解决方法,但是通常,您希望视图模型层以视图易于使用的格式准备数据。为了提高性能,让我们将所有选择的日期包装在查找表中:

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。实际上,如果我们确实愿意的话,可以将逻辑放置在视图模型中,但是它不引用任何特定于视图模型的数据,并且可以在其他地方重复使用,因此我们将规则一点点:

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();
    }
}

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.