问题
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.
回答1:
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?
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();
}
Update
Please 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;
}
来源:https://stackoverflow.com/questions/56174152/how-to-know-calendarviewdayitem-is-out-of-calendarview-month-scope