Search code examples
.netwindowsxamlwinui-3winui

Removing the strikethrough line in CalendarView in WinUI


I am implementing a custom control based on CalendarView and have an undesired strikethrough line going through today, like this:

Strikethrough line on a calendar

My CalendarViewDayItemStyle is defined as following:

<CalendarView.CalendarViewDayItemStyle>
    <Style TargetType="CalendarViewDayItem">
        <Setter Property="FontSize" Value="12" />
        <Setter Property="FontWeight" Value="ExtraLight" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <StackPanel VerticalAlignment="Bottom">
                            <Grid
                                Padding="5"
                                Margin="4,0,4,4"
                                VerticalAlignment="Bottom">
                                <Grid.Background>
                                    <SolidColorBrush Color="{Binding RegularSeasonMarkerColor.Color}" Opacity="1"></SolidColorBrush>
                                </Grid.Background>
                                <TextBlock Text="{Binding RegularSeasonText}" TextDecorations="None"></TextBlock>
                            </Grid>

                            <Grid
                                Padding="5"
                                Margin="4,0,4,0"
                                VerticalAlignment="Bottom">
                                <Grid.Background>
                                    <SolidColorBrush Color="{Binding CSRSeasonMarkerColor.Color}" Opacity=".3"></SolidColorBrush>
                                </Grid.Background>
                                <TextBlock Text="{Binding CSRSeasonText}" TextDecorations="None"></TextBlock>
                            </Grid>
                        </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</CalendarView.CalendarViewDayItemStyle>

How can I remove the strikethrough without also changing IsTodayHighlighted, which would also remove the highlight for the current day?

EDIT (6/20/2024): Here is the opening configuration for CalendarView:

<CalendarView
    x:Name="CalendarViewControl"
    IsTodayHighlighted="true"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    CalendarItemBorderBrush="DimGray"
    CalendarItemBorderThickness="1"
    CalendarItemCornerRadius="0"
    BlackoutStrikethroughBrush="Transparent"
    BlackoutBackground="#5B6163"
    BlackoutForeground="#2F2F2F"
    DayItemFontSize="{StaticResource BodyTextBlockFontSize}"
    DayItemMargin="4,0,0,0"
    IsGroupLabelVisible="True"
    FirstOfMonthLabelFontSize="{StaticResource BodyTextBlockFontSize}"
    FirstOfMonthLabelFontWeight="Bold"
    HorizontalFirstOfMonthLabelAlignment="Right"
    FirstOfMonthLabelMargin="0,0,4,0"
    DayItemFontWeight="SemiBold"
    FirstDayOfWeek="Monday"
    HorizontalDayItemAlignment="Left"
    VerticalDayItemAlignment="Top">

Solution

  • The CalendarView has

    • TodayBlackoutForeground
    • TodayBlackoutBackground

    but it doesn't have

    • TodayBlackoutStrikethroughBrush

    To remove blackout strikethrough lines, you can set

    <CalendarView BlackoutStrikethroughBrush="Transparent" .../>
    

    But with the item for today, you need to do this manually. At least for the moment. For example:

    private void CalendarViewControl_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
    {
        if (args.Item.Date.Date != DateTime.Now.Date ||
            args.Item.FindDescendant<Line>() is not Line todayItemBlackoutLine)
        {
            return;
        }
    
        todayItemBlackoutLine.Visibility = Visibility.Collapsed;
    }
    

    FindDescendant() comes from the CommunityToolkit.WinUI.Extensions NuGet package.