DataTrigger不处理嵌套的依赖项属性

时间:2017-06-28 11:11:30

标签: c# wpf triggers datatrigger

我有以下课程($arrAutoloadLibraries = ["database","user_agent"]; if ($_COOKIE['isLoggedIn'] == 1) { $arrAutoloadLibraries[] = "session"; } $autoload['libraries'] = $arrAutoloadLibraries; TextListClass)。

C#代码

TextEntryClass

以及以下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 }

XAML Style

Style

每当选择新的<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的{​​{1}}无效(触发器未触发)。

1 个答案:

答案 0 :(得分:3)

尝试{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>