Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 816
mjk6026 Avatar asked Aug 06 '11 14:08

mjk6026


1 Answers

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.

like image 182
Mario Vernari Avatar answered Oct 21 '22 19:10

Mario Vernari