本文介绍了如何知道 CalendarViewDayItem 超出 CalendarView 月份范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在 CalendarView 日元素中进行一些样式设置,我需要知道 CalendarViewDayItem(日期)是否在当前 CalendarView 月份的范围内.有没有办法获得当前的月份/年份?

这就是我到目前为止所得到的:一个样式化的 CalendarView 和 CalendarViewDayItem 可以在日历中显示一些视觉信息,尽管只有天数.10 月(9 月 30 日和 11 月的日期)之后的所有 CalendarViewDayItems 都应该着色/降低对比度.日期的天数带有阴影(CalendarView OutOfSCOpe 样式值)而不是 CalendarViewDayItem 元素.

这里是样式化的 CalendarViewDayItem 代码:​​

最后,更改 CalendarViewDayItem 外观的 c# 代码:

 private void calviewSun_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args){//渲染基本的日项目.if (args.Phase == 0){//为下一阶段注册回调.//今天之前的每个日期都变灰if (args.Item.Date < DateTimeOffset.Now.AddDays(-1)){args.Item.Opacity = 0.5;}别的{args.Item.Opacity = 1.0;}args.RegisterUpdateCallback(calviewSun_CalendarViewDayItemChanging);}}

我没有包含 CalendarView 代码,因为它太长了,而且我认为这个问题并不是那么无趣.

解决方案

你可以处理

更新请尝试使用 CalendarViewDayItemChanging 处理程序获取日期,如下所示.

private void CalendarView_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args){var item = args.Item;var date = item.Date;}

To do some styling in CalendarView day elements, I need to know if CalendarViewDayItem (date) is in current CalendarView month's scope or not. Is there any way to get the current month/year?

This is what I got so far: An styled CalendarView and CalendarViewDayItem to show in the calendar some visual info despite of only day number. All the CalendarViewDayItems out of October (sept 30 and the dates of novemeber) should be shaded/lowered contrast. Date's day number is shaded (CalendarView OutOfSCope styling values) but not the CalendarViewDayItem elements.

Here the styled CalendarViewDayItem code:

<Style TargetType="CalendarViewDayItem" >
            <Setter Property="MinWidth"         Value="40"/>
            <Setter Property="MinHeight"        Value="40"/>
            <Setter Property="Margin"           Value="0"/>
            <Setter Property="BorderThickness"  Value="2"/>
            <Setter Property="Padding"          Value="0, 0, 0, 4"/>
            <Setter Property="FontSize"         Value="10"/>
            <Setter Property="FontWeight"       Value="SemiBold"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="CalendarViewDayItem">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>

                            <!--Header phase?-->
                            <Grid x:Name="DayItemHeader" Grid.Row="0" Margin="0,25,0,0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <!--<TextBlock x:Name="DayItemDateNumber" Grid.Column="0" Text="a"/>-->
                                <TextBlock x:Name="DayItemPhase" Grid.Column="1" Text="Phase"/>
                            </Grid>

                            <Grid x:Name="DayItemBody" Grid.Row="1" Background="Transparent">
                                <Image Height="30" Width="30" Margin="0" Source="{Binding Icon}" Stretch="UniformToFill" />

                                <StackPanel>
                                    <Image Height="30" Width="30" Margin="0" Source="{Binding Icon}" Stretch="UniformToFill" />
                                    <TextBlock x:Name="tbSomeText" Text="Some text"/>
                                </StackPanel>

                            </Grid>

                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>




  <CalendarView x:Name="calviewDatePicker"
                          HorizontalAlignment="Center" VerticalAlignment="Center"
                          Height="600" Width="600"

                            SelectionMode="Single" IsGroupLabelVisible="True"
                            IsOutOfScopeEnabled="True" Language="en-US"
                            CalendarIdentifier="GregorianCalendar"

                            OutOfScopeBackground="{StaticResource VeryDarkGreyThemeColor}" OutOfScopeForeground="{ThemeResource MediumGreyThemeColor}"
                            CalendarItemBackground="{ThemeResource VeryDarkGreyThemeColor}" CalendarItemForeground="{ThemeResource VeryLightGreyThemeColor}"
                            SelectedBorderBrush="{ThemeResource PrimaryThemeColor}" SelectedPressedBorderBrush="{x:Null}" SelectedHoverBorderBrush="{ThemeResource PrimaryThemeColor}"
                            BorderBrush="{x:Null}" Background="{ThemeResource VeryDarkGreyThemeColor}"
                            FirstDayOfWeek="Monday" NumberOfWeeksInView="6" Style="{StaticResource CalendarViewStyle1}" DayOfWeekFormat="{}{dayofweek.solo.abbreviated(3)}"

                          FirstOfMonthLabelFontWeight="ExtraBlack"
                          VerticalFirstOfMonthLabelAlignment="Top" HorizontalFirstOfMonthLabelAlignment="Left"
                          VerticalDayItemAlignment="Top" HorizontalDayItemAlignment="Right"

                          CalendarViewDayItemChanging="calviewSun_CalendarViewDayItemChanging"
                          />

And finally, the c# code that changes the CalendarViewDayItem appearence:

 private void calviewSun_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
    {

        // Render basic day items.
        if (args.Phase == 0)
        {
            // Register callback for next phase.

            // Every date before today is grayed
            if (args.Item.Date < DateTimeOffset.Now.AddDays(-1))
            {
                args.Item.Opacity = 0.5;
            }
            else
            {
                args.Item.Opacity = 1.0;
            }

            args.RegisterUpdateCallback(calviewSun_CalendarViewDayItemChanging);

        }

    }

I haven't included CalendarView code because it's too long and I think it's not that ineteresting for the problem.

解决方案

you could handle SelectedDatesChanged event and get the select date from args. and the args contains current month/year.

MyCalendarView.SelectedDatesChanged += MyCalendarView_SelectedDatesChanged;

private void MyCalendarView_SelectedDatesChanged(CalendarView sender, CalendarViewSelectedDatesChangedEventArgs args)
{
    MyTextBox.Text = args.AddedDates.First().ToString();
}

UpdatePlease try to get date with CalendarViewDayItemChanging handler like the follow.

private void CalendarView_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
{
    var item = args.Item;
    var date = item.Date;
}

这篇关于如何知道 CalendarViewDayItem 超出 CalendarView 月份范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 10:54