在模板中访问WPF控件.问题与CalendarDayButton

本文关键字:控件 问题 CalendarDayButton WPF 访问 | 更新日期: 2023-09-27 18:17:10

我试图访问Template中的Control。为此,我重新定义了控件CalendarDayButton:

<Window.Resources>
    <Style x:Key="myStyleDayButtonCalendar" TargetType="{x:Type CalendarDayButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CalendarDayButton}">
                    <Grid  Name="gridCalendar">
                        <ContentControl Margin="5,1,5,1" Content="{TemplateBinding Content}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid x:Name="LayoutRoot" Background="White">
    <Calendar CalendarDayButtonStyle="{StaticResource myStyleDayButtonCalendar}" Name="myCalendar"  SelectedDatesChanged="Calendar_SelectedDatesChanged_1" />
</Grid>

这是可以的。但是当我想访问在我的控制网格。不可能:

private void Calendar_SelectedDatesChanged_1(object sender, SelectionChangedEventArgs e)
{
    Grid gridInTemplate = (Grid)myCalendar.Template.FindName("gridCalendar", myCalendar) as Grid;
}

我的网格是null。所以,我尝试了另一个Control。带Button:

<Window.Resources>
   <Style x:Key="myStyleButton" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid  Name="myButton">
                        <Ellipse Fill="DarkBlue"></Ellipse>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid x:Name="LayoutRoot" Background="White">
    <Calendar CalendarDayButtonStyle="{StaticResource myStyleDayButtonCalendar}" Name="myCalendar"  SelectedDatesChanged="Calendar_SelectedDatesChanged_1" />
    <Button Style="{StaticResource myStyleButton}" Name="myButton2" Margin="92,99,518,338" Click="myButton2_Click_1"></Button>
</Grid>

背后代码:

private void myButton2_Click_1(object sender, RoutedEventArgs e)
{
    Grid gridInTemplate = (Grid)myButton2.Template.FindName("myButton", myButton2);
}

这里gridInTemplate是NOT NULL。为什么在这种情况下,dayCalendarButton gridInTemplate是NULL ?我想避免使用VisualTreeHelper .

在模板中访问WPF控件.问题与CalendarDayButton

在您的例子中,您试图在Calendar中找到CalendarDayButton。但这是两个不同的控件。例如,我们有一个日历样式:

    <Style TargetType="{x:Type Calendar}">            
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="BorderBrush" Value="Gray" />
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Calendar}">
                    <StackPanel x:Name="PART_Root" HorizontalAlignment="Center">
                        <CalendarItem x:Name="PART_CalendarItem" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

在代码中,我们可以像这样访问StackPanel:

    private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
    {
        StackPanel StackPanelInTemplate = (StackPanel)MyCalendar.Template.FindName("PART_Root", MyCalendar) as StackPanel;          
        MessageBox.Show(StackPanelInTemplate.Name);     
    }

但它是在日历样式不存在的CalendarDayButton。这是一个单独的控件。在CalendarDayButton中,我们只能得到style:

Style MyCalendarDayButton = (Style)MyCalendar.CalendarDayButtonStyle;       

但不是模板。形式的结构:

CalendarDayButton MyCalendarDayButton = FindChild<CalendarDayButton>(MyCalendar, "gridCalendar");   

它不会工作。无法工作,原因是它与日历不在同一可视树中。

结论:也许只是访问CalendarDayButton将不起作用,因为它不是在日历的可视化树中看到(已经设计为开发人员)。虽然我可能是错的。我在使用DatePicker时遇到了类似的问题。不能简单地访问DatePicker的某些部分,例如:访问水印- http://matthamilton.net/datepicker-watermark.

决定取决于你为什么需要这个按钮。尝试将您的功能移动到控件,样式或创建自己的控件,从CalendarDayButton类继承(这很烦人)。我们使用转换器,例如:

<Grid x:Name="CalendarDayButtonGrid">                         
    <Grid.ToolTip>
          <MultiBinding Converter="{StaticResource HighlightDate}">
              <MultiBinding.Bindings>
                  <Binding />
                    <Binding RelativeSource="{RelativeSource FindAncestor, 
                               AncestorType={x:Type Calendar}}" />
              </MultiBinding.Bindings>
          </MultiBinding>
    </Grid.ToolTip>
    <!-- End addition -->
</Grid>

注:这里告诉您如何使用WPF ToolKit访问CalendarDayButton: http://codesticks.wordpress.com/tag/wpf-toolkit/