How to change ancestor's property with a trigger on template child
I have a combobox and I have templat开发者_如何学Pythoned its control template. Togglebutton is a part of the control template. Now when a event is raised on the togglebutton(ex: Checked), I want to change combobox property(ex:Foreground). How to do this? Can we do this using relativesource?
You can use a Trigger
in the ControlTemplate
and use the Trigger.SourceName
property to specify the ToggleButton
as the source of the trigger.
Example:
<ControlTemplate ... >
<Grid ... >
<!-- ... -->
<ToggleButton x:Name="btnToggle" ... />
<!-- ... -->
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="btnToggle" Property="IsChecked" Value="True">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Of course, this only applies to properties and not events, as you requested. However, there are many properties which can be used instead of the events, like the IsChecked
property instead of the Checked
event.
精彩评论