gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:52:53 25 4
gpt4 key购买 nike

我想将日期列表绑定(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com