8

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?

H.B.
  • 166,899
  • 29
  • 327
  • 400
mjk6026
  • 1,484
  • 3
  • 20
  • 38
  • Where is that image (or control that you mean)? – Mario Vernari Aug 06 '11 at 14:13
  • Basically, i want to change a another control's proeprty in a same window. But also, the control can place in the application resource, window's resource, control's resource, too. – mjk6026 Aug 06 '11 at 14:21

1 Answers1

13

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.

Mario Vernari
  • 6,649
  • 1
  • 32
  • 44