2013-05-21 19 views
2

我正在構建一個繼承自標準System.Windows.Controls.Calendar控件的自定義控件。我正在關注代碼項目的這篇文章。我也想讓CalendarItem子對象可以調整大小,就像這篇MSDN上的文章一樣。如何使用綁定從標準控件引用控件模板?

我從Calendar控件的源代碼中將Calendar,CalendarDayButton和CalendarItem的樣式複製到位於我的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 --> 

現在,因爲該綁定使用一個靜態資源,我需要將樣式的三個按鈕複製到我的ResourceDictionary 。我不打算對這些按鈕做任何更改,所以我希望能夠避免在ResourceDictionary中爲它們設置樣式。

有沒有什麼辦法可以通過綁定來引用這些按鈕的原始樣式?

回答

1

是的,有:

在從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 
} 

,並從內部綁定到這些樣式您的ControlTemplate爲使用TemplatedParent綁定的日曆。

<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> 

然後,我會定義可能的最高水平,該按鈕模板(PreviousButtonTemplate等)分配到自定義日曆控件的依賴屬性默認樣式。儘可能達到最高水平,我的意思是這個對象,在其範圍內存在所有模板,並將它們連接在一起,例如app.xaml或您的窗口。

<Style TargetType="viewModels:MyCalendar"> 
    <Setter Property="PreviousButtonTemplate" Value="{StaticResource PreviousButtonTemplate}" /> 
</Style> 

或者,你可以離開它,因爲它是,並且只要StaticResources,該控件模板是指在運行過程中存在的,它們將被正確解析。不好,雖然沒有設計時間支持。

編輯

好吧,我沒有得到你指的是「原始」的按鈕模板,我以爲你說你已經定義在其他地方的模板。然後:不,你不能這樣做。我已經多次嘗試過同樣的事情,但不能創建使用原始模板的控件模板。 ControlTemplates沒有'BasedOn'這樣的東西。

+0

嗨馬克,這是一個非常有趣的技術,使用依賴屬性。但我不認爲它解決了任何問題。使用你的方法,我仍然需要爲這些按鈕創建一個樣式/控件模板,對它們的綁定是否完全不同?我正在尋找的是從Controls.Calendar控件引用現有模板的方法。我可能誤解了一些東西,當然:) – Cousken

+1

好吧,我沒有得到你指的是'原始'按鈕模板,我以爲你的意思是你已經在其他地方定義的模板。然後:不,你不能這樣做。我已經多次嘗試過同樣的事情,但不能創建使用原始模板的控件模板。 ControlTemplates沒有'BasedOn'這樣的東西。 – Marc

+0

謝謝Marc :)我相信儘可能多地使用代碼,所以我非常希望有這樣的可能性。 – Cousken