如何解决在日历控件中的日期按钮内添加一个椭圆可单击以表示WPF中的特殊日期
目标:要在任何特殊日期下添加一个小圆点(椭圆),并且该圆点也应是可选的。想法是传递一个特定的日期,然后在该天画一个椭圆。
进度:我在日期下方添加了椭圆。
请检查代码是否正确。
Xaml:
<Window.Resources>
<local:CustomLetterDayConverter x:Key="CustomLetterDayConverter" />
<Style x:Key="CalendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource CustomLetterDayConverter}}" Value="{x:Null}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CalendarDayButton">
<Border Background="{TemplateBinding Background}">
<StackPanel Orientation="Vertical">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" TextBlock.Foreground="{TemplateBinding Foreground}"/>
<Ellipse Height="5" Width="5" Fill="Silver" Stretch="Uniform"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange" SelectedDate="{Binding SelectedDate}"
CalendarDayButtonStyle="{StaticResource CalendarDayButtonStyle}"
>
<Calendar.BlackoutDates>
<CalendarDateRange Start="01-09-2020" End="05-09-2020"/>
</Calendar.BlackoutDates>
</Calendar>
</Grid>
</Window>
隐藏代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class CustomLetterDayConverter : IValueConverter
{
static HashSet<DateTime> dict = new HashSet<DateTime>();
static CustomLetterDayConverter()
{
dict.Add(DateTime.Today);
dict.Add(DateTime.Today.AddDays(1));
dict.Add(DateTime.Today.AddDays(2));
dict.Add(DateTime.Today.AddDays(5));
}
public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
{
string text = null;
if (dict.Contains((DateTime)value))
text = null;
else
text = "";
return text;
}
public object ConvertBack(object value,CultureInfo culture)
{
throw new NotImplementedException();
}
}
按照此解决方案,我可以添加特殊日期。但这是无法选择的。
谢谢。
解决方法
当您覆盖控件的Template
时,您将负责实现控件所需的所有可视状态。
在您的情况下,您仅实现了ContentPresenter
,而您的Ellipse
则没有触发器来处理CalendarDayButton
的许多其他视觉状态。
要查看需要实现的内容,应使用Blend for Visual Studio,将Calendar控件添加到示例项目中,然后提取控件模板,以便可以看到完整的参考模板。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。