WPF : Chainging another control's property in some control's Trigger

12,461

You must aggregate the source and the target in some way.

You may create either a custom control containing both the hyperlink/textblock and the image. That's the preferred way if you have several blocks that behaves in such example.

If you don't like this. You may create a "temporary" anonymous control as follows:

<ControlTemplate x:Key="myCtl" TargetType="ContentControl">
  <StackPanel>
    <Image x:Name="img"/>
    <ContentPresenter x:Name="ctr" />
  </StackPanel>

  <ControlTemplate.Triggers>
                    <Trigger SourceName="ctr" Property="IsMouseOver" Value="True">
                        <Setter TargetName="ctr" Property="Foreground" Value="RoyalBlue"/>
                        <!--I like to insert a code at here that changes another control's property...-->
                    </Trigger>
                    <Trigger SourceName="ctr" Property="IsMouseOver" Value="False">
                        <Setter TargetName="ctr" Property="Foreground" Value="#FF808080"/>
                        <!--..and this line too.-->
                   </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

The above xaml will reside in your Window's resources.

NOTE: it's much like a track to follow, than a fully functional snippet!

In the body, you may refer the control in such manner:

<ContentControl Template="{StaticResource myCtl}" Content="this is my text block" />

Hope it helps.

Share:
12,461

Related videos on Youtube

mjk6026
Author by

mjk6026

hi.

Updated on June 04, 2022

Comments

  • mjk6026
    mjk6026 almost 2 years

    Here's my textblock.

        <Image x:Name:imgAnother/>
    
        <TextBlock>
            this is my text block
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="TextDecorations" Value="None"/>
                    <Style.Triggers>
                        <Trigger Property="TextBlock.IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="RoyalBlue"/>
                            <!--I like to insert a code at here that changes another control's property...-->
                        </Trigger>
                        <Trigger Property="TextBlock.IsMouseOver" Value="False">
                            <Setter Property="Foreground" Value="#FF808080"/>
                            <!--..and this line too.-->
                       </Trigger>
                    </Style.Triggers>                    
                </Style>
            </TextBlock.Style>
        </TextBlock>
    

    I like to make a xaml code which can change another control's proerpty, like "imgAnother".

    how can i do that?