TabControl标头中的TextBlock上的MouseLeftButtonDown事件未触发

时间:2014-04-15 15:42:27

标签: wpf xaml

我有一个像这样定义的TabControl:

<TabControl x:Class="MyNamespace.TabContainer"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"            
     ItemsSource="{Binding}">

<TabControl.Resources>
    <Style TargetType="TextBox">
        <Setter Property="VerticalAlignment" Value="Stretch"></Setter>
        <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
        <Setter Property="AcceptsReturn" Value="True"></Setter>
        <Setter Property="TextWrapping" Value="WrapWithOverflow"></Setter>
        <Setter Property="MaxLines" Value="5000"></Setter>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"></Setter>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"></Setter>
    </Style>

    <SolidColorBrush x:Key="mouseOverColor" Color="Red"></SolidColorBrush>
    <RadialGradientBrush x:Key="glowColor" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5" GradientOrigin="0.5,0.5">
        <GradientStop Color="Red" Offset="0.5"></GradientStop>
    </RadialGradientBrush>
    <RadialGradientBrush x:Key="glowMask" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5" GradientOrigin="0.5,0.5">
        <GradientStop Color="#80FFFFFF" Offset="0"></GradientStop>
        <GradientStop Color="#80FFFFFF" Offset="0.6"></GradientStop>
        <GradientStop Color="#00FFFFFF" Offset="1"></GradientStop>
    </RadialGradientBrush>

    <DataTemplate x:Key="TabHeader" DataType="TabItem">
        <DockPanel>
            <Button Name="btn_TabDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" Click="btn_TabDelete_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}">
                <Image Source="icon_grey_cross.gif" Height="9" Width="9" Margin="0,0,0,0"></Image>
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="bdr_main" Margin="4" BorderThickness="0" >
                            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="4,4,4,4" ContentSource="Content" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="bdr_main" Property="Background" Value="{StaticResource glowColor}"></Setter>
                                <Setter TargetName="bdr_main" Property="OpacityMask" Value="{StaticResource glowMask}"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <TextBlock MouseLeftButtonDown="UIElement_OnMouseLeftButtonDown" VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Header}"></TextBlock>
        </DockPanel>
    </DataTemplate>
</TabControl.Resources>

除了一件事,它工作得很好;单击TextBlock时,OnMouseLeftButtonDown事件永远不会触发。

要明确的是,当我说它“永远不会开火”时我的基础是我在UIElement_OnMouseLeftButtonDown()的第一行放置一个断点,当我单击选项卡标题中的文本时,没有点击断点。使用PreviewMouseLeftButtonDown效果不错。

我在这里出错了什么?它看起来应该非常简单明了。

1 个答案:

答案 0 :(得分:0)

DataTemplates内的事件不会将您的事件连接到正确的处理程序。创建后,您需要为每个文本块手动附加事件。

<强>更新

您必须访问每个Template的{​​{1}}属性,然后致电TabItem为您的FindName提供一个名称,以便找到它。

鉴于TextBlock tabItem

TabItem