DataTrigger 不适用于嵌套依赖项 属性

DataTrigger not working on nested Dependency Property

我有以下 类(TextListClassTextEntryClass)。

C# 代码

public class TextListClass : Control
{
    /// <summary>
    /// The Selected Text
    /// </summary>
    public TextEntryClass SelectedText
    {
        get { return (TextEntryClass)GetValue(SelectedTextProperty); }
        set { SetValue(SelectedTextProperty, value); }
    }

    /// <summary>
    /// The <see cref="SelectedText"/> DependencyProperty.
    /// </summary>
    public static readonly DependencyProperty SelectedTextProperty = DependencyProperty.Register("SelectedText", typeof(TextEntryClass), typeof(TextListClass), new PropertyMetadata(null));

    public TextListClass()
    {
        DefaultStyleKey = typeof(TextListClass);
    }
}

public class TextEntryClass : Control
{
    /// <summary>
    /// Text
    /// </summary>
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    /// <summary>
    /// The <see cref="Text"/> DependencyProperty.
    /// </summary>
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TextEntryClass), null);

    /// <summary>
    /// The Type
    /// </summary>
    public ETextEntryType Type
    {
        get { return (ETextEntryType)GetValue(TypeProperty); }
        set { SetValue(TypeProperty, value); }
    }

    /// <summary>
    /// The <see cref="Type"/> DependencyProperty.
    /// </summary>
    public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type", typeof(ETextEntryType), typeof(TextEntryClass), new PropertyMetadata(ETextEntryType.One));

    public TextEntryClass()
    {
        DefaultStyleKey = typeof(TextEntryClass);
    }
}

public enum ETextEntryType
{
    One,
    Two
}

以及以下 Style

XAML风格

<Style TargetType="Controls:TextListClass">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Controls:TextListClass">
                <StackPanel>
                    <FooControl x:Name="fooControlName"/>
                    <ContentPresenter Content="{TemplateBinding SelectedText}"/>
                </StackPanel>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=SelectedText.Type}" Value="{x:Static Controls:ETextEntryType.One}">
                        <Setter TargetName="fooControlName" Property="FooProperty" Value="FooValue"/>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Whenever a new TextEntryClass is selected, the new Text is shown (got a seperate DataTemplate. Omit to keep the survey).但我也想更改 FooControlFooProperty 不工作(触发器未触发)。

尝试 {RelativeSource Self}:

<DataTrigger Binding="{Binding Path=SelectedText.Type, RelativeSource={RelativeSource Self}}" 
                                         Value="{x:Static Controls:ETextEntryType.One}">
    <Setter TargetName="fooControlName" Property="FooProperty" Value="FooValue"/>
</DataTrigger>