我正在开发UWP Win10 VS2015应用程序。我已经自定义了Calendar Control,但需要实现以下功能。
点按任意日期,应以圆形填充圆圈突出显示。
在多个日期点按并滑动手指,应选择该日期范围。
是否有任何Visualstates或其他事件放在Style(ControlTemplate)中并操纵它以滑动手指,当它应该突出显示另一个日期的界限时。 ???或者应该在这里应用什么程序:)
请参阅以下4个屏幕截图。 (这些只是样本镜头,我需要这种类型的功能)
根据上面的屏幕截图...这是我认为的自定义功能,可以编辑样式和模板,并且可以在样式中放置一些操纵,点击和拖动事件......但是如何放置这些并且至少得到了这个功能的想法......我将非常感激。感谢。
查看动画图片,并将其与顶部给出的其他图片进行比较...当我们点击任何项目时,边框的背景应变为蓝色,文字即日期应变为 WHITE ,如上图所示。实际上CalendarviewDayItem样式中没有ContentPresenter或ItemPresenter ...所以plz放了这个功能。感谢。
Alhamdulillah我们现在非常接近我们的目标......并且InshaAllah可以进行"范围选择"功能,所以我想引用一些主题,这些主题肯定有助于我们进行多重选择"特征。 :)
VisualTreeHelper.FindElementsInHostCoordinates(Point, UIElement) method
UIElement.FindSubElementsForTouchTargeting method
所以,如果你检查这些主题。您将获得帮助以在手指滑动InshaAllah上实现多重选择功能:)
答案 0 :(得分:1)
calendarViewDayItem的Xaml样式
<Style x:Key="CalendarViewDayItemStyle1" TargetType="CalendarViewDayItem">
<Setter Property="MinWidth" Value="40"/>
<Setter Property="MinHeight" Value="40"/>
<Setter Property="Margin" Value="1"/>
<Setter Property="Padding" Value="0, 0, 0, 4"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CalendarViewDayItem">
<Grid >
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CustomStates">
<VisualState x:Name="Hover">
<VisualState.Setters>
<Setter Target="ContentPresenter.(Border.Background)" Value="Blue"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Target="ContentPresenter.(Border.Background)" Value="White"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="ContentPresenter" PointerPressed="border_PointerPressed" PointerEntered="border_PointerEntered" BorderBrush="Red" PointerExited="border_PointerExited" PointerMoved="border_PointerMoved" BorderThickness="1,1,1,1" CornerRadius="10,10,10,10" >
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
代码背后
CalendarViewDayItem item;
private void CalendarView_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
{
var item = args.Item;
item.PointerPressed += Item_PointerPressed;
item.Tapped += Item_Tapped;
item.PointerEntered += Item_PointerEntered;
item.PointerExited += Item_PointerExited;
}
private void Item_PointerExited(object sender, PointerRoutedEventArgs e)
{
item = null;
}
private void Item_PointerEntered(object sender, PointerRoutedEventArgs e)
{
item = sender as CalendarViewDayItem;
}
private void Item_Tapped(object sender, TappedRoutedEventArgs e)
{
item = sender as CalendarViewDayItem;
(sender as CalendarViewDayItem).Background = new SolidColorBrush(Colors.Red);
}
private void Item_PointerPressed(object sender, PointerRoutedEventArgs e)
{
item = sender as CalendarViewDayItem;
}
private void border_PointerEntered(object sender, PointerRoutedEventArgs e)
{
if (item != null)
{
VisualStateManager.GoToState((item), "Hover", true);
}
}
private void border_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (item != null)
{
VisualStateManager.GoToState((item), "Hover", true);
}
}
private void border_PointerExited(object sender, PointerRoutedEventArgs e)
{
if (item != null)
{
VisualStateManager.GoToState((item), "Normal", true);
}
}
private void border_PointerPressed(object sender, PointerRoutedEventArgs e)
{
if (item != null)
{
VisualStateManager.GoToState((item), "Hover", true);
}
}
<强>更新强>
只需使用以下方法将所选方法设为蓝色。删除代码背后的代码
private void CalendarView_SelectedDatesChanged(CalendarView sender, CalendarViewSelectedDatesChangedEventArgs args)
{
if(args.AddedDates!=null)
{
foreach(var item in args.AddedDates)
{
var selected = FindElementInVisualTree<CalendarViewDayItem>(sender, item);
}
}
if (args.RemovedDates != null)
{
foreach (var item in args.RemovedDates)
{
}
}
}
public static T FindElementInVisualTree<T>(DependencyObject parentElement,DateTimeOffset selectedDate) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0) return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is CalendarViewDayItem)
{
if((child as CalendarViewDayItem).Date==selectedDate.DateTime)
{
VisualStateManager.GoToState((child as CalendarViewDayItem), "Hover", true);
}
else if ((child as CalendarViewDayItem).Date.Date == DateTime.Today)
{
// VisualStateManager.GoToState((child as CalendarViewDayItem), "Hover", true);
//styles for today's date
}
else
{
VisualStateManager.GoToState((child as CalendarViewDayItem), "Normal", true);
}
}
else
{
var result = FindElementInVisualTree<T>(child,selectedDate);
if (result != null)
return result;
}
}
return null;
}
当我们点击任何项目时,边框的背景应变为蓝色,文字即日期应变为白色,如上图所示
日历控件有很多属性。搜索此 PressedForeground 并将其值更改为白色,并浏览其他类似的属性