C# 仅针对TabItem的选项卡 问题是:

C# 仅针对TabItem的选项卡 问题是:,c#,wpf,drag-and-drop,tabcontrol,C#,Wpf,Drag And Drop,Tabcontrol,我正在创建一个拖放界面,允许在一个选项卡内移动项目,以及从一个选项卡移动到另一个选项卡。现在,我有以下样式,它允许我在选项卡项中定位标签。由于与资源字典冲突,我想通过代码隐藏添加事件。问题是,如果我将事件添加到选项卡项中,那么它不仅会触发选项卡本身,还会触发选项卡内容 问题是: 我将如何在下面的代码中连接事件 <Style x:Key="DragDropTi" BasedOn="{StaticResource {x:Type TabItem}}" TargetTyp

我正在创建一个拖放界面,允许在一个选项卡内移动项目,以及从一个选项卡移动到另一个选项卡。现在,我有以下
样式
,它允许我在
选项卡项
中定位
标签
。由于与
资源字典
冲突,我想通过代码隐藏添加
事件
。问题是,如果我将
事件
添加到
选项卡项
中,那么它不仅会触发选项卡本身,还会触发选项卡内容

问题是: 我将如何在下面的代码中连接事件

<Style
    x:Key="DragDropTi"
    BasedOn="{StaticResource {x:Type TabItem}}"
    TargetType="TabItem"> 
    <Setter
        Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <Label
                    Foreground="White"
                    Content="{Binding}">
                    <Label.Style>
                        <Style
                            TargetType="Label">
                            <EventSetter
                                Event="Drop"
                                Handler="TabItemDrop" />
                            <Setter
                                Property="AllowDrop"
                                Value="True" />
                        </Style>
                    </Label.Style>
                </Label>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

答案比预期的简单。我只需将
MenuItem.Header
设置为
标签,而不是名称字符串,并将
事件
附加到
标签

Label header = new Label();
header.Content = "Tab Name";
header.AllowDrop = true;
header.Drop += new DragEventHandler(TabItemDrop);

TabItem tabItem = new TabItem();
tabItem.Header = header;