wpf HeaderedContentControl 在 contenttemplate 上设置事件处理程序

wpf HeaderedContentControl set event handler on contenttemplate

如何在资源文件中定义的 ContentTemplate 上设置事件处理程序?

window xaml

<!-- this doesnt get triggered -->
 <Style x:Key="TabControlEventSetter" TargetType="{x:Type TabControl}">
    <EventSetter Event="SelectionChanged" 
     Handler="TabControl_SelectionChanged"/>
 </Style>

<!-- headered content control containing tab control -->
<HeaderedContentControl
  x:Name="WorkspaceTabControl"
  Header="Workspaces"
  Style="{StaticResource MainHCCStyle}"    
  Content="{Binding Path=Workspaces}"
  ContentTemplate="{StaticResource WorkspacesTemplate}"
  />

资源xaml

 <!-- configure content area -->
 <Style x:Key="MainHCCStyle" TargetType="{x:Type HeaderedContentControl}">
    <Setter Property="HeaderTemplate">
       <Setter.Value>
           <DataTemplate>
               <TextBlock Text="{TemplateBinding Content}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
       <Setter.Value>
           <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
               <DockPanel>
                   <ContentPresenter
                       DockPanel.Dock="Top"
                       ContentSource="Header"
                       ContentTemplate="{TemplateBinding HeaderTemplate}"
                   />
                   <ContentPresenter
                      ContentSource="Content"
                      ContentTemplate="{TemplateBinding ContentTemplate}"
                   />
               </DockPanel>
          </ControlTemplate>
     </Setter.Value>
   </Setter>
</Style>

<!-- Render a Tab Control -->
<DataTemplate x:Key="TabItemTemplate">
    <DockPanel >
       <ContentPresenter
          Content="{Binding Path=DisplayName}"
          VerticalAlignment="Center"
         />
     </DockPanel>
</DataTemplate>

<!-- Render Workspace -->
<DataTemplate x:Key="WorkspacesTemplate">
    <TabControl
        IsSynchronizedWithCurrentItem="True"
        ItemTemplate="{StaticResource TabItemTemplate}"
        ItemsSource="{Binding}"
        >
        <TabControl.Resources>
            <Style TargetType="TabItem">
                 <Setter Property="Template">
                     <Setter.Value>
                         <ControlTemplate TargetType="TabItem">
                              <ContentPresenter
                                   x:Name="ContentSite"
                                    ContentSource="Header"
                                />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.Resources>
        </TabControl>
    </DataTemplate>

windowxamlcs

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   if (sender is TabItem tab)
      {
      // handle tab change event
      }
}

尝试按照此处的建议定义样式... Is there Selected Tab Changed Event in the standard WPF Tab Control 这似乎不起作用。

那么,xaml 中有没有一种方法可以将事件处理程序“附加”到在单独资源 xaml 中定义的“contentTemplate”?

如果您将 Style 定义为没有 x:Key 的隐式控件,它将应用于范围内没有任何特定 [=11] 的所有 TabControl 控件=] 应用于他们:

<HeaderedContentControl
  x:Name="WorkspaceTabControl"
  Header="Workspaces"
  Style="{StaticResource MainHCCStyle}" 
  Content="{Binding Path=Workspaces}"
  ContentTemplate="{StaticResource WorkspacesTemplate}">
    <HeaderedContentControl.Resources>
        <Style TargetType="{x:Type TabControl}">
            <EventSetter Event="SelectionChanged" 
                         Handler="TabControl_SelectionChanged"/>
        </Style>
    </HeaderedContentControl.Resources>
</HeaderedContentControl>