接上篇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方法中調用