加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何绑定WPF工具包中的日程表日历控件?

发布时间:2020-12-15 17:38:58 所属栏目:百科 来源:网络整理
导读:我想绑定一个日期列表到BlackoutDates属性,但它似乎是不可能的.特别是在MVVM场景中.有没有人完成这样的事情?有没有任何良好的日历控件,使MVVM播放很好? 解决方法 对于您的DatePicker困境,我发现使用附加属性(从我使用CommandBindings修改)的整洁的黑客: c
我想绑定一个日期列表到BlackoutDates属性,但它似乎是不可能的.特别是在MVVM场景中.有没有人完成这样的事情?有没有任何良好的日历控件,使MVVM播放很好?

解决方法

对于您的DatePicker困境,我发现使用附加属性(从我使用CommandBindings修改)的整洁的黑客:
class AttachedProperties : DependencyObject
{

    #region RegisterBlackoutDates

    // Adds a collection of command bindings to a date picker's existing BlackoutDates collection,since the collections are immutable and can't be bound to otherwise.
    //
    // Usage: <DatePicker hacks:AttachedProperties.RegisterBlackoutDates="{Binding BlackoutDates}" >

    public static DependencyProperty RegisterBlackoutDatesProperty = DependencyProperty.RegisterAttached("RegisterBlackoutDates",typeof(System.Windows.Controls.CalendarBlackoutDatesCollection),typeof(AttachedProperties),new PropertyMetadata(null,OnRegisterCommandBindingChanged));

    public static void SetRegisterBlackoutDates(UIElement element,System.Windows.Controls.CalendarBlackoutDatesCollection value)
    {
        if (element != null)
            element.SetValue(RegisterBlackoutDatesProperty,value);
    }
    public static System.Windows.Controls.CalendarBlackoutDatesCollection GetRegisterBlackoutDates(UIElement element)
    {
        return (element != null ? (System.Windows.Controls.CalendarBlackoutDatesCollection)element.GetValue(RegisterBlackoutDatesProperty) : null);
    }
    private static void OnRegisterCommandBindingChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e)
    {
        System.Windows.Controls.DatePicker element = sender as System.Windows.Controls.DatePicker;
        if (element != null)
        {
            System.Windows.Controls.CalendarBlackoutDatesCollection bindings = e.NewValue as System.Windows.Controls.CalendarBlackoutDatesCollection;
            if (bindings != null)
            {
                element.BlackoutDates.Clear();
                foreach (var dateRange in bindings)
                {
                    element.BlackoutDates.Add(dateRange);
                }
            }
        }
    }

    #endregion
}

我相信我太晚了,不能帮助你,但希望别人会觉得有用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读