我的样式/控制模板超出了我的日历的限制日期
我的 C#/WPF 项目需要日历。我们将使用它来选择约会安排的日期范围。我被告知默认日历太小,我们的一些代表无法使用,所以我一直在努力调整它的大小。
<toolkit:Calendar Grid.Row="1" x:Name="DateWindowCalendar"
BorderBrush="White" BorderThickness="0"
Style="{StaticResource PopupCalendarStyle}"
DisplayMode="Month" SelectionMode="SingleRange"
DisplayDateStart="{Binding FirstDayOfMonth}"
AutomationProperties.AutomationId="ToolkitCalendarId"
VerticalAlignment="Top">
</toolkit:Calendar>
我创造了这样的造型:
<Style x:Key="PopupCalendarStyle" TargetType="toolkit:Calendar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="toolkit:Calendar">
<StackPanel Margin="0" HorizontalAlignment="Center" x:Name="Root">
<toolkit:Calendar x:Name="Calendar"
SelectedDate="{TemplateBinding SelectedDate}"
DisplayDateStart="{TemplateBinding DisplayDateStart}"
SelectionMode="{TemplateBinding SelectionMode}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SelectedDatesChanged="Calendar_SelectedDatesChanged">
<toolkit:Calendar.CalendarDayButtonStyle>
<Style>
<Setter Property="Button.Height" Value="34"/>
<Setter Property="Button.Width" Value="34" />
<Setter Property="Button.FontSize" Value="16" />
</Style>
</toolkit:Calendar.CalendarDayButtonStyle>
<toolkit:Calendar.CalendarButtonStyle>
<Style>
<Setter Property="Button.Height" Value="34"/>
<Setter Property="Button.Width" Value="34"/>
<Setter Property="Button.FontSize" Value="16"/>
</Style>
</toolkit:Calendar.CalendarButtonStyle>
</toolkit:Calendar>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
一切都近乎完美。我指定了我的范围,我可以跟踪选定的日期(使用 SelectedDatesChanged 事件而不是 SelectedDates 属性授予。
问题是我还需要能够设置限制日期(通常是月初到今天之间的时间段,尽管有时是第一天)本月到几天后)。
如果没有样式,这是有效的:
DateWindowCalendar.BlackoutDates.Add(new CalendarDateRange(
new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01), DateTime.Now));
但是当我添加样式时,我不会显示黑色显示,更糟糕的是,我无法选择黑色日期
。 这样我就不必重建整个小部件,
。
谢谢克里斯
确定我错过了什么,但我希望有人能给出一个简单的答案,
My C#/WPF project needs a calendar. We're going to be using it to pick a range of dates for appointment scheduling. I'm told the default calendar is too small to be used by some of our reps so I've been working on resizing it.
<toolkit:Calendar Grid.Row="1" x:Name="DateWindowCalendar"
BorderBrush="White" BorderThickness="0"
Style="{StaticResource PopupCalendarStyle}"
DisplayMode="Month" SelectionMode="SingleRange"
DisplayDateStart="{Binding FirstDayOfMonth}"
AutomationProperties.AutomationId="ToolkitCalendarId"
VerticalAlignment="Top">
</toolkit:Calendar>
And I've created this styling:
<Style x:Key="PopupCalendarStyle" TargetType="toolkit:Calendar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="toolkit:Calendar">
<StackPanel Margin="0" HorizontalAlignment="Center" x:Name="Root">
<toolkit:Calendar x:Name="Calendar"
SelectedDate="{TemplateBinding SelectedDate}"
DisplayDateStart="{TemplateBinding DisplayDateStart}"
SelectionMode="{TemplateBinding SelectionMode}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SelectedDatesChanged="Calendar_SelectedDatesChanged">
<toolkit:Calendar.CalendarDayButtonStyle>
<Style>
<Setter Property="Button.Height" Value="34"/>
<Setter Property="Button.Width" Value="34" />
<Setter Property="Button.FontSize" Value="16" />
</Style>
</toolkit:Calendar.CalendarDayButtonStyle>
<toolkit:Calendar.CalendarButtonStyle>
<Style>
<Setter Property="Button.Height" Value="34"/>
<Setter Property="Button.Width" Value="34"/>
<Setter Property="Button.FontSize" Value="16"/>
</Style>
</toolkit:Calendar.CalendarButtonStyle>
</toolkit:Calendar>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Everything is almost perfect. I specify my range, I can track the selected dates (granted using the SelectedDatesChanged event instead of the SelectedDates property.
The problem is I also need to be able to set blackout dates (usually the period between first of the month and today although sometimes first of the month to a few days from now).
Without the styling, this works:
DateWindowCalendar.BlackoutDates.Add(new CalendarDateRange(
new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01), DateTime.Now));
But when I add the style, I don't get the black out displays being displayed and worse its possible to select the blackout dates.
I'm not sure what I missed but I'm hoping someone has an easy answer so that I don't have to rebuild the entire widget.
Any help is appreciated.
Thanks,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以你的风格,我看不出你在哪里尊重“停电日期”。基本风格是什么样的?我假设在基本样式中,日历控件上有一个名为 Blackout Dates 的属性,该属性绑定到 BlackoutDates 集合。
像这样的东西:
In your style I don't see where you are respecting "Blackout dates." What does the base style look like? I assume in the base Style there is an attribute on the Calendar control called blackout dates that is binding to the BlackoutDates Collection.
Something like this :
好吧……那不在其中,但我会把这个答案留给下一个人。
First BlackoutDates 是只读范围集合的一些可怕的大杂烩。几乎无法绑定。我尝试重写这个小部件,但没有走得太远。
我确实找到了有用的东西。您可以在代码中设置停电范围。
但是,如果您在构造期间执行此操作,则它不会通过模板传递(不知道为什么)。因此,如果您构建一个日历,然后设置有效的停电范围。但是,当您设置该日历的样式时,它没有模板绑定(因为它不是依赖属性,因此无法绑定)。
但是...看起来如果您通过样式生成的事件设置 BlackoutDates,则日期将被设置。我这样使用了加载事件:
完成后,创建一个加载处理程序相当简单,它让我可以在样式化的日历上设置 BlackoutDates:
Okay... that wasn't in it but I'll leave this answer for the next person.
First BlackoutDates is some horrid mish-mash of a readonly collection of ranges. Pretty much unbindable. I poked around with rewriting the widget but didn't get all that far.
I did find something that works. You can set the blackout range in code.
However, if you do this during construction it isn't passed through the template (not sure why). So if you construct a Calendar, and then set the blackout range that works. But when you style that Calendar it doesn't have template bindings (because it isn't a dependency property it can't be bound).
However... it looks like if you set the BlackoutDates through an event generated by the style, then the dates are set. I used the loaded event as so:
With that done it was fairly trivial to create a load handler that let me set the BlackoutDates on the styled Calendar: