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

前端之家收集整理的这篇文章主要介绍了c# – 如何绑定WPF工具包中的日程表日历控件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想绑定一个日期列表到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
}

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

猜你在找的C#相关文章