TabItem.Header 中的扩展器占用了点击量

Expander in TabItem.Header is eating the click

我想要做的是让 tabitem 内容仅在 TabItem IsSelected 和 Expander IsExpanded 时可见

我现在遇到的问题是:
- 如果 Expander IsHitTestVisible="False" 然后 TabItem IsSelected 但 Expander 不展开 (/Collapse)
- 如果 Expander IsHitTestVisible="True" 那么 TabItem 不是 已选中但扩展器确实展开(/折叠)

我需要 IsHitTestVisible="False" 和 IsHitTestVisible="True" 的行为。我怎样才能实现这种行为?

我知道Expander中没有真正的内容
我只想在转换器中使用 IsExpanded 的值(以及上下箭头)

我想要的是扩展器将点击传递给 TabItem

<TabItem x:Name="tabitem3">
    <TabItem.Header>
        <Expander Header="Three" x:Name="tabexp3"/>
    </TabItem.Header>
    <TextBlock Text="Content Three TabItem"  Background="Honeydew" >
        <TextBlock.Visibility>
            <MultiBinding Converter="{StaticResource bvc2}" Mode="OneWay">
            <Binding ElementName="tabexp3" Path="IsExpanded"/>
            <Binding ElementName="tabitem3" Path="IsSelected" />
            </MultiBinding>
        </TextBlock.Visibility>
    </TextBlock>
</TabItem

public class VisabilityConverterTwoBool : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //if (values == null) return Visibility.Visible;
        //if (values.Length != 2) return Visibility.Visible;
        if (values.Length != 2) return Visibility.Collapsed;
        if (values[0] as bool? == null || values[1] as bool? == null)
        {
            Debug.WriteLine(values[0].ToString());
            Debug.WriteLine(values[1].ToString());
            return Visibility.Collapsed;
        }
        //if (values.Length != 2 && values[0] as bool? == null || values[1] as bool? == null) return Visibility.Visible;
        try
        {
            Debug.WriteLine(((bool)values[0]).ToString() + " " + ((bool)values[1]).ToString());
            if ((bool)values[0] && (bool)values[1]) return Visibility.Visible;
            else return Visibility.Collapsed;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message, "VisabilityConverterTwoBool");
            Debug.WriteLine(values[0].ToString());
            Debug.WriteLine(values[1].ToString());
        }
        return Visibility.Visible;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

试试下面的代码。

<TabControl>
        <TabItem Header="First">

        </TabItem>
        <TabItem x:Name="tabitem3">
            <TabItem.Header>
                <Expander Header="Three" x:Name="tabexp3" IsExpanded="{Binding Path=IsSelected, ElementName=tabitem3,Mode=TwoWay}"/>
            </TabItem.Header>
            <TextBlock Text="Content Three TabItem"  Background="Honeydew" >
                <TextBlock.Visibility>
                    <MultiBinding Converter="{StaticResource bvc2}" Mode="OneWay">
                        <Binding ElementName="tabexp3" Path="IsExpanded"/>
                        <Binding ElementName="tabitem3" Path="IsSelected" />
                    </MultiBinding>
                </TextBlock.Visibility>
            </TextBlock>
        </TabItem>
    </TabControl>

我是通过展开器上的展开和折叠事件完成的

private void expcolp(object sender, RoutedEventArgs e)
{
    if (manualCollapse) return;
    if (!(sender is Expander)) return;
    Expander exp = (Expander)sender;                                    
    var d = sender as DependencyObject;
    DependencyObject parent = VisualTreeHelper.GetParent(d);
    while (parent != null)
    {
        if (parent is TabItem)
        {
            TabItem tabitem = (TabItem)parent;
            if (!tabitem.IsSelected)
            {
                tabitem.IsSelected = true;
                if (!exp.IsExpanded) exp.IsExpanded = true;
            }
            break;
        }
        d = parent as DependencyObject;
        parent = VisualTreeHelper.GetParent(d);
    }
}