I have the following classes (TextListClass
and 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
}
and the follwoing 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>
Whenever a new TextEntryClass
is selected, the new Text
is shown (got a seperate DataTemplate
. Omit to keep the survey). But I also want to change the FooProperty
of the FooControl
which is not working (The Trigger is not triggered).
Try {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>