DataTrigger无法在嵌套依赖属性上工作



我有以下类( 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
}

和follwoing 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>

每当选择新的TextEntryClass时,都会显示新的Text(获得了单独的DataTemplate。省略以保持调查(。但是我也想更改不起作用的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>

最新更新