【问题标题】:WPF Calendar - Highlights Dates of CommitmentsWPF 日历 - 突出承诺日期
【发布时间】:2020-04-28 00:47:50
【问题描述】:

我正在尝试用“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】:

    你做错了。使用 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();
        }
    }
    

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

    【讨论】:

    • 您知道如何使用 Caliburn.Micro 进行此调用吗?
    • 该绑定对 Caliburn.Micro 的工作方式应该完全相同,除非有其他问题。
    • 很遗憾,由于某种原因,Caliburn Micro 无法连接。最终返回此错误:i.imgur.com/sGQZ6br.png" =[
    • 如果您将MCVE 作为单独的问题发布并在此处链接到它,那么我会看看。我自己试过了,效果很好,所以你的绑定设置方式一定是别的东西。
    • 早上好,感谢您的帮助。我针对发生的问题创建了一个新帖子,我也将它发送到 guithub 以更清楚。 stackoverflow.com/questions/61559500/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多