.net 如何使用绑定从标准控件引用控件模板?
我正在生成从标准System.Windows.Controls.Calendar控件继承的自定义控件。我正在关注这篇关于代码项目的文章。我还想使CalendarItem子对象的大小可调整,如本文中关于MSDN的内容 我将Calendar、CalendarDayButton和CalendarItem的样式从Calendar控件的源代码复制到位于my Generic.xaml中的自定义控件ResourceDictionary中。CalendarItem样式引用3个按钮模板(上个月、下个月和标题按钮),如下所示:.net 如何使用绑定从标准控件引用控件模板?,.net,wpf,xaml,custom-controls,controltemplate,.net,Wpf,Xaml,Custom Controls,Controltemplate,我正在生成从标准System.Windows.Controls.Calendar控件继承的自定义控件。我正在关注这篇关于代码项目的文章。我还想使CalendarItem子对象的大小可调整,如本文中关于MSDN的内容 我将Calendar、CalendarDayButton和CalendarItem的样式从Calendar控件的源代码复制到位于my Generic.xaml中的自定义控件ResourceDictionary中。CalendarItem样式引用3个按钮模板(上个月、下个月和标题按钮)
<!-- Start: Previous button content -->
<Button x:Name="PART_PreviousButton"
Grid.Row="0" Grid.Column="0"
Template="{StaticResource PreviousButtonTemplate}"
Height="20" Width="28"
HorizontalAlignment="Left"
Focusable="False"
/>
<!-- End: Previous button content -->
<!-- Start: Header button content -->
<Button x:Name="PART_HeaderButton"
Grid.Row="0" Grid.Column="1"
Template="{StaticResource HeaderButtonTemplate}"
HorizontalAlignment="Center" VerticalAlignment="Center"
FontWeight="Bold" FontSize="10.5"
Focusable="False"
/>
<!-- End: Header button content -->
<!-- Start: Next button content -->
<Button x:Name="PART_NextButton"
Grid.Row="0" Grid.Column="2"
Height="20" Width="28"
HorizontalAlignment="Right"
Template="{StaticResource NextButtonTemplate}"
Focusable="False"
/>
<!-- End: Next button content -->
现在,由于此绑定使用StaticResource,我需要将这三个按钮的样式复制到我的ResourceDictionary中。我不打算对这些按钮做任何更改,所以我希望可以避免在我的ResourceDictionary中为它们设置样式
有没有办法通过绑定来引用这些按钮的原始样式?有,有:
在继承自Calendar
的自定义控件上,可以为三种样式中的每一种公开类型为ControlTemplate
的依赖项属性
public class MyCalender : Calendar
{
public static readonly DependencyProperty PreviousButtonTemplateProperty =
DependencyProperty.Register("PreviousButtonTemplate", typeof (ControlTemplate), typeof (MyCalender), new PropertyMetadata(default(ControlTemplate)));
public ControlTemplate PreviousButtonTemplate
{
get { return (ControlTemplate) GetValue(PreviousButtonTemplateProperty); }
set { SetValue(PreviousButtonTemplateProperty, value); }
}
// Same for other button templates
}
并使用TemplatedParent
绑定从日历的ControlTemplate
中绑定到这些样式
<ControlTemplate x:Key="MyCalender" TargetType="viewModels:MyCalendar">
<Grid>
<Button x:Name="PART_PreviousButton"
Grid.Row="0" Grid.Column="0"
Template="{TemplateBinding Property=PreviousButtonTemplate}"
Height="20" Width="28"
HorizontalAlignment="Left"
Focusable="False"
/>
</Grid>
</ControlTemplate>
或者,您可以保持原样,只要控件模板引用的StaticResources
在运行时存在,它们就会正确解析。虽然不太好,也没有设计时支持
编辑
好的,我不明白你指的是“原始”按钮模板,我以为你指的是你已经在其他地方定义的模板。然后:不,你不能这样做。我已经尝试了很多次同样的事情,但是你不能创建使用部分原始模板的控件模板。ControlTemplates没有“BasedOn”这类东西。Hi Marc,这是一种使用依赖属性的非常有趣的技术。但我认为它解决不了任何问题。使用你的方法,我仍然需要为按钮创建一个样式/控制模板,除非它们的绑定方式非常不同?我要寻找的是一种从Controls.Calendar控件引用现有模板的方法。当然,我可能误解了什么:)好吧,我不明白你指的是“原始”按钮模板,我以为你指的是你已经在其他地方定义的模板。然后:不,你不能这样做。我已经尝试了很多次同样的事情,但是你不能创建使用部分原始模板的控件模板。ControlTemplates没有“BasedOn”这个词。谢谢Marc:)我相信尽可能多地重用代码,所以我非常希望有这种可能性。我也是,这是一种痛苦,特别是在设计更复杂的控件时。理解具有可视状态等的复杂控件模板可能需要一天的时间。。不管怎样,祝你好运。
<Style TargetType="viewModels:MyCalendar">
<Setter Property="PreviousButtonTemplate" Value="{StaticResource PreviousButtonTemplate}" />
</Style>