2012-02-23 83 views
0

我正在尝试向.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"/> 

回答

0

看一看VisualTreeHelper Class,特别是GetParent方法

由于日历是你的按钮的父母,你将不再需要在树上下来上去,然后再次,所以你不应该使用GetChild

+0

日历是父级?我认为这个stackpanel是,这使得它变得更容易:) – 2012-02-23 21:39:02

+0

对不起,让我换个日历是按钮的祖先。因此,您可以在循环中调用“VisualTreeHelper.GetParent”,直到获得日历 – Terkel 2012-02-23 22:22:21

0

在你的点击处理程序的参数中,你有'发件人'(对象):这是来自发件人,你想找到父类与VisualTreeHelper类。