如何从WPF日历中设置BlackoutDates的样式

时间:2014-06-27 12:32:27

标签: wpf xaml

我想为WPF日历的BlackoutDates设置样式。默认可视化是日期数字上方的灰色十字。例如,如何仅将它们变灰?

1 个答案:

答案 0 :(得分:6)

你去吧

<Calendar>
    <Calendar.CalendarDayButtonStyle>
        <Style TargetType="CalendarDayButton" BasedOn="{StaticResource {x:Type CalendarDayButton}}">
            <Style.Triggers>
                <Trigger Property="IsBlackedOut" Value="True">
                    <Setter Property="Background" Value="LightGray"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Calendar.CalendarDayButtonStyle>
    <Calendar.BlackoutDates>
        <CalendarDateRange Start="24-June-2014" End="25-June-2014"/>
    </Calendar.BlackoutDates>
</Calendar>

我已为CalendarDayButtonStyle指定了自定义样式,并在该样式中我在IsBlackedOut上定义了触发器,并将Background设置为LightGray

结果

result

您可以根据需要选择操纵其他属性

删除现有的停电

要删除现有的停电,您可以选择为日历日按钮定义自定义模板,但缺点是您必须为每个主题维护它,例如Luna,Classic等

我将提出一个可以帮助您利用现有模板的解决方案,因此无需担心此类问题

我使用了相同的附加属性

class CalenderHelper

namespace CSharpWPF
{
    class CalenderHelper : DependencyObject
    {
        public static bool GetIsBlackOutDisabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsBlackOutDisabledProperty);
        }

        public static void SetIsBlackOutDisabled(DependencyObject obj, bool value)
        {
            obj.SetValue(IsBlackOutDisabledProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsBlackOutDisabled.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsBlackOutDisabledProperty =
            DependencyProperty.RegisterAttached("IsBlackOutDisabled", typeof(bool), typeof(CalenderHelper), new PropertyMetadata(false, OnIsBlackOutDisabledChanged));

        private static void OnIsBlackOutDisabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CalendarDayButton dayButton = d as CalendarDayButton;
            if (dayButton.IsLoaded)
            {
                SetBlackout(dayButton, (bool)e.NewValue);
            }
            else
            {
                dayButton.Loaded += (s, ee) =>
                {
                    SetBlackout(dayButton, (bool)e.NewValue);
                };
            }
        }

        static void SetBlackout(CalendarDayButton dayButton, bool collapsed)
        {
            ControlTemplate template = dayButton.Template;
            Path blackoutPath = template.FindName("Blackout", dayButton) as Path;
            if (collapsed)
                blackoutPath.Visibility = System.Windows.Visibility.Collapsed;
            else
                blackoutPath.Visibility = System.Windows.Visibility.Visible;
        }
    }
}

我创建了一个带有附加属性CalenderHelper的类IsBlackOutDisabled,它将隐藏现有模板中的中断元素

XAML

<Calendar x:Name="cal" xmlns:l="clr-namespace:CSharpWPF">
    <Calendar.CalendarDayButtonStyle>
        <Style TargetType="CalendarDayButton" BasedOn="{StaticResource {x:Type CalendarDayButton}}">
            <Style.Triggers>
                <Trigger Property="IsBlackedOut" Value="True">
                    <Setter Property="Background" Value="LightGray"/>
                    <Setter Property="l:CalenderHelper.IsBlackOutDisabled" Value="True"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Calendar.CalendarDayButtonStyle>
    <Calendar.BlackoutDates>
        <CalendarDateRange Start="24-June-2014" End="25-June-2014"/>
    </Calendar.BlackoutDates>
</Calendar>

我在触发器中启用了我新创建的属性l:CalenderHelper.IsBlackOutDisabled,这反过来会隐藏默认的停电视觉

结果

result

这种方法使其灵活用于许多目的,您可以将其编写为其他复杂功能,这些功能通过简单的xaml不易实现