c# - 如何在 WPF 工具包日历控件中绑定(bind) BlackoutDates?

标签 c# wpf mvvm data-binding calendar

我想将日期列表绑定(bind)到 BlackoutDates 属性,但这似乎不太可能。特别是在 MVVM 场景中。有没有人完成过这样的事情?是否有适合 MVVM 的良好日历控件?

最佳答案

对于你的 DatePicker 难题,我发现了一个使用附加属性的巧妙 hack(根据我对 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# - 如何在 WPF 工具包日历控件中绑定(bind) BlackoutDates?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1638128/

相关文章:

c# - Windows Phone 8 上的 X509Certificate2 到 X509Certificate

c# - 使工具提示区域大于控件

c# - MVVM View 模型异步数据初始化

javascript - 仅从 HighCharts 禁用打印图表选项

c# - 用于 ComboBox Item Selected 的事件处理程序(Selected Item 不一定更改)

c# - 从 Parallel.ForEach 循环内部调用异步方法时出现无效操作异常

c# - 为什么我会收到 System.InvalidOperationException?

wpf - WPF和Windows Form是否有一些类似于div的控件行为

c# - 如何在 CLI::C++ 中使用委托(delegate) - 匿名委托(delegate)?

android - Kotlin接口(interface)无法实例化!接口(interface)名称 : kotlinx. 协程.Deferred