views:

29

answers:

1

I have a combobox and I have templated 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?

+1  A: 

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.

gehho
Appreciate your answer.However when it triggers I want the property of Combobox to be changed(like opacity or foreground) and not togglebutton's property. Is there anyway to refer the combobox from togglebutton?
funwithcoding
Actually, I assumed that the above code would set the `ComboBox`'s `Foreground` to `Red` and not the `ToggleButton`'s. *Did you try the code?* Normally, if you do not specify a `TargetName` in the `Setter`, it is applied to the templated control, in this case the `ComboBox`. However, this might not be true if the `Trigger`'s `SourceName` property is used. But you should still give it a try if you did not do so yet!
gehho