我正在嘗試向.net 4.0 DatePicker日曆添加一個按鈕,並且已將按鈕放置到位,但無法弄清楚如何爲其授予對日曆的訪問權限。我正在使用的指南是通過在_Click方法中使用此引用來完成的,但是當我這樣做時,「this」是指按鈕而不是日曆。所以我的想法是使用GetParent,然後GetChild獲取對日曆的引用。但GetParent不能從我這裏得到。有任何想法嗎?從事件方法內訪問父項
感謝
<Style x:Key="DatePickerCalendarStyle" TargetType="{x:Type Calendar}" BasedOn="{StaticResource {x:Type Calendar}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Calendar}">
<Border BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}">
<StackPanel Orientation="Vertical">
<StackPanel x:Name="PART_Root" HorizontalAlignment="Center">
<CalendarItem x:Name="PART_CalendarItem"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Style="{TemplateBinding CalendarItemStyle}" />
<Button x:Name="PART_TodayButton"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="10,0,10,2"
Content="Today"
Click="PART_TodayButton_Click"/>
日曆是父級?我認爲這個stackpanel是,這使得它變得更容易:) – 2012-02-23 21:39:02
對不起,讓我換個日曆是按鈕的祖先。因此,您可以在循環中調用「VisualTreeHelper.GetParent」,直到獲得日曆 – Terkel 2012-02-23 22:22:21