0

我有以下问题:我需要为嵌套模板控件(按钮)的事件设置一个监听器。我无法得到它。我尝试了一些诸如覆盖 OnApplyTemplate 之类的事情,但我只能获得顶级模板控件,但不能从嵌套模板中获得控制。具体我需要“btnfirstPage”控件的实例样式如下:

<ControlTemplate x:Key="DataGridPager" TargetType="{x:Type controls:DataGrid}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
        BorderThickness="{TemplateBinding BorderThickness}"
        SnapsToDevicePixels="True" Padding="{TemplateBinding Padding}">
  <ScrollViewer Focusable="false" Name="DG_ScrollViewer">
    <ScrollViewer.Template>
      <ControlTemplate x:Name="ScrollViewerTemplate" TargetType="{x:Type ScrollViewer}">
        <Grid Name="ScrollViewerGrid"
          DataContext="{Binding Path=ItemsSource, RelativeSource={RelativeSource AncestorType={x:Type controls:DataGrid}}}">
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="35" />
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
          </Grid.ColumnDefinitions>
          <!--Left Column Header Corner -->
          <Button Style="{StaticResource DataGridSelectAllStyle}"
                  Width="{Binding Path=CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type controls:DataGrid}}}"
                  Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:DataGrid}}, Path=HeadersVisibility, 
                                                    Converter={x:Static controls:DataGrid.HeadersVisibilityConverter}, 
                                                    ConverterParameter={x:Static DataGridHeadersVisibility.All}}" />
          <!--Column Headers-->
          <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1"
                                          Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=HeadersVisibility, 
                                                                            Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
                                                                            ConverterParameter={x:Static DataGridHeadersVisibility.Column}}" />
          <!--DataGrid content-->
          <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.Row="1"
                                  Tag="{Binding Path=Tag, RelativeSource={RelativeSource AncestorType={x:Type controls:DataGrid}}}"
                                  Grid.ColumnSpan="2" CanContentScroll="{TemplateBinding CanContentScroll}" />
          <ScrollBar Grid.Row="1" Grid.Column="2" Name="PART_VerticalScrollBar" Orientation="Vertical"
                     Maximum="{TemplateBinding ScrollableHeight}" ViewportSize="{TemplateBinding ViewportHeight}"
                     Value="{Binding Path=VerticalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                     Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" />
          <Grid Grid.Row="2" Grid.Column="1">
            <Grid.ColumnDefinitions>
              <ColumnDefinition
                Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:DataGrid}}, Path=NonFrozenColumnsViewportHorizontalOffset}" />
              <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <ScrollBar Grid.Column="1" Name="PART_HorizontalScrollBar" Orientation="Horizontal"
                       Maximum="{TemplateBinding ScrollableWidth}" ViewportSize="{TemplateBinding ViewportWidth}"
                       Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                       Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" />
          </Grid>
          <StatusBar Grid.Row="3" Grid.ColumnSpan="3" Name="DataGridStatusBar"
                     Visibility="{Binding PagerVisibility, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type controls:DataGrid}, Mode=FindAncestor}}">
            <StatusBar.Resources>
              <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Margin" Value="5" />
              </Style>
            </StatusBar.Resources>
            <StatusBarItem>
              <StackPanel Orientation="Horizontal">
                <StackPanel.Resources>
                  <Style TargetType="{x:Type Button}">
                    <Setter Property="Width" Value="20" />
                    <Setter Property="FontSize" Value="9" />
                  </Style>
                </StackPanel.Resources>
                <Button x:Name="BtnFirstPage" Margin="3" Command="{Binding PagerWrapper.PagingFirstCommand}"
                        ToolTip="{x:Static localization:Captions.PagerFirst}">

                  <Image
                    Source="/KTN.Framework.Presentation.Shared;component/Resources/Images/NewLook/16/navigate_left2.png" />
                </Button>
                <Button Name="BtnPrevPage" Margin="3" Command="{Binding PagerWrapper.PagingPrevCommand}"
                        ToolTip="{x:Static localization:Captions.PagerPrevious}">
                  <Image
                    Source="/KTN.Framework.Presentation.Shared;component/Resources/Images/NewLook/16/navigate_left.png" />
                </Button>
                <Button Name="BtnNextPage" Margin="3" Command="{Binding PagerWrapper.PagingNextCommand}"
                        ToolTip="{x:Static localization:Captions.PagerNext}">
                  <Image
                    Source="/KTN.Framework.Presentation.Shared;component/Resources/Images/NewLook/16/navigate_right.png" />
                </Button>
                <Button Name="BtnLastPage" Margin="3" Command="{Binding PagerWrapper.PagingLastCommand}"
                        ToolTip="{x:Static localization:Captions.PagerLast}">
                  <Image
                    Source="/KTN.Framework.Presentation.Shared;component/Resources/Images/NewLook/16/navigate_right2.png" />
                </Button>
              </StackPanel>
            </StatusBarItem>
            <StatusBarItem>
              <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding PagerWrapper.Page}" />
                <TextBlock Text="/" />
                <TextBlock Text="{Binding PagerWrapper.TotalPages}" />
              </StackPanel>
            </StatusBarItem>
            <StatusBarItem>
              <StackPanel Orientation="Horizontal">
                <TextBlock Text="{x:Static localization:Captions.PagerRecords}" />
                <TextBlock Text="{Binding PagerWrapper.TotalRecords}" />
              </StackPanel>
            </StatusBarItem>
            <StatusBarItem>
              <TextBlock>
                <TextBlock.Style>
                  <Style TargetType="TextBlock">
                    <Style.Triggers>
                      <DataTrigger Binding="{Binding Path=FilterStatus}" Value="">
                        <Setter Property="Visibility" Value="Collapsed" />
                      </DataTrigger>
                      <DataTrigger Binding="{Binding Path=FilterStatus}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Collapsed" />
                      </DataTrigger>
                    </Style.Triggers>
                  </Style>
                </TextBlock.Style>
                <Run Text="Filtered on:" />
                <Run Text="{Binding Path=FilterStatus}" />
              </TextBlock>
            </StatusBarItem>
          </StatusBar>
        </Grid>
      </ControlTemplate>
    </ScrollViewer.Template>
    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
  </ScrollViewer>
</Border>

4

0 回答 0