接上篇
wpf日历控件制作过程分析(1)---定义header
在header中,我们看到了定义一个
自定义样式TitleStyle
1.自定义样式
看后台代码定义
Code
/**//// <summary>
/// The DependencyProperty for the TitleStyle property.
/// Flags: none
/// Default Value: null
/// </summary>
public static readonly DependencyProperty TitleStyleProperty =
DependencyProperty.Register(
"TitleStyle",
typeof(Style),
typeof(MonthCalendar),
new FrameworkPropertyMetadata(
(Style)null));
/**//// <summary>
/// TitleStyle property
/// </summary>
public Style TitleStyle
{
get { return (Style)GetValue(TitleStyleProperty); }
set { SetValue(TitleStyleProperty, value); }
}
应该说,是比较简单的,默认样式为null,如果指定了样式的话,则会覆盖默认的样式
2.不重叠选择样式
为日历的前进和后退按钮定义两个样式
Code
/**//// <summary>
/// The DependencyProperty for the PreviousButtonStyle property.
/// Flags: none
/// Default Value: null
/// </summary>
public static readonly DependencyProperty PreviousButtonStyleProperty =
DependencyProperty.Register(
"PreviousButtonStyle",
typeof(Style),
typeof(MonthCalendar),
new FrameworkPropertyMetadata(
(Style)null, new PropertyChangedCallback(OnPreviousButtonStyleChanged)));
/**//// <summary>
/// PreviousButtonStyle property
/// </summary>
public Style PreviousButtonStyle
{
get { return (Style)GetValue(PreviousButtonStyleProperty); }
set { SetValue(PreviousButtonStyleProperty, value); }
}
private static void OnPreviousButtonStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MonthCalendar)d).RefreshPreviousButtonStyle();
}
/**//// <summary>
/// The DependencyProperty for the NextButtonStyle property.
/// Flags: none
/// Default Value: null
/// </summary>
public static readonly DependencyProperty NextButtonStyleProperty =
DependencyProperty.Register(
"NextButtonStyle",
typeof(Style),
typeof(MonthCalendar),
new FrameworkPropertyMetadata(
(Style)null, new PropertyChangedCallback(OnNextButtonStyleChanged)));
/**//// <summary>
/// NextButtonStyle property
/// </summary>
public Style NextButtonStyle
{
get { return (Style)GetValue(NextButtonStyleProperty); }
set { SetValue(NextButtonStyleProperty, value); }
}
private static void OnNextButtonStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MonthCalendar)d).RefreshNextButtonStyle();
}
再来这个
RefreshNextButtonStyle方法
Code
private void RefreshNextButtonStyle()
{
ButtonBase nextButton = GetTemplateChild(c_NextButtonName) as ButtonBase;
if (nextButton != null)
{
if (NextButtonStyle == null)
{
if (_defaultNextButtonStyle == null)
{
_defaultNextButtonStyle = FindResource(new ComponentResourceKey(typeof(MonthCalendar), "NextButtonStyleKey")) as Style;
}
nextButton.Style = _defaultNextButtonStyle;
}
else
{
nextButton.Style = NextButtonStyle;
}
}
}
注意点:
1.
GetTemplateChild可以获取到xaml中声明了key值的元素,c_NextButtonName变量就是xaml定义的key,此key应该公开给控件的使用者,可调整样式
2.用
TemplatePart公开模块样式,如两个按钮的key值
[TemplatePart(Name = "PART_PreviousButton", Type = typeof(ButtonBase))]
[TemplatePart(Name = "PART_NextButton", Type = typeof(ButtonBase))]
3.
_defaultNextButtonStyle还是从xaml去找,其目的是为了自定义样式不会与默认样式发生冲突.可以title样式比较
4.构建可视化模板需要在
OnApplyTemplate方法中使用,如上面的
RefreshNextButtonStyle方法必须在
OnApplyTemplate方法中调用