开发者

How to change ancestor's property with a trigger on template child

开发者 https://www.devze.com 2023-01-20 11:59 出处:网络
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 c

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消