C# 从事件方法中访问父级

C# 从事件方法中访问父级,c#,wpf,calendar,datepicker,parent-child,C#,Wpf,Calendar,Datepicker,Parent Child,我正在尝试向.net 4.0 DatePicker日历中添加一个按钮,我已将该按钮放置到位,但无法确定如何授予它访问日历的权限。我正在使用的指南是通过在_Click方法中使用this reference来实现的,但当我这样做时,“this”指的是按钮而不是日历。所以我的想法是使用GetParent和GetChild来获取日历的引用。但是GetParent在“this”之外对我不可用。有什么想法吗 谢谢 <Style x:Key="DatePickerCalendarStyle" Targe

我正在尝试向.net 4.0 DatePicker日历中添加一个按钮,我已将该按钮放置到位,但无法确定如何授予它访问日历的权限。我正在使用的指南是通过在_Click方法中使用this reference来实现的,但当我这样做时,“this”指的是按钮而不是日历。所以我的想法是使用GetParent和GetChild来获取日历的引用。但是GetParent在“this”之外对我不可用。有什么想法吗

谢谢

<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"/>
请看一看,特别是GetParent方法


由于日历是按钮的父级,因此不需要在树中先上后下,因此不应在单击处理程序的参数中使用GetChild,因为您有“sender”对象:这是来自要使用VisualTreeHelper类查找父级的发件人。

日历是父级吗?我认为stackpanel是,这使它更容易:对不起,让我重新表述一下日历是按钮的祖先。因此,您可以在循环中调用VisualTreeHelper.GetParent,直到获得日历