2011-08-06 166 views
7

これは私のテキストブロックです。WPF:一部のコントロールのトリガーで別のコントロールのプロパティを呼び出す

<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> 

"imgAnother"のように別のコントロールのproerptyを変更できるxamlコードを作成するのが好きです。

どうすればいいですか?

+0

その画像はどこですか? –

+0

基本的に、私は同じウィンドウ内の別のコントロールのproeprtyを変更したい。しかし、コントロールは、アプリケーションリソース、ウィンドウのリソース、コントロールのリソースに配置することもできます。 – mjk6026

答えて

12

何らかの方法でソースとターゲットを集約する必要があります。

ハイパーリンク/テキストブロックと画像の両方を含むカスタムコントロールを作成することができます。このような例で動作するブロックがいくつかある場合は、これが好ましい方法です。

これが気に入らない場合は、

<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> 

上記のxamlは、あなたのウィンドウのリソースに常駐します。

注:これは完全に機能的なスニペットよりも、追従するトラックに似ています!ボディには

、あなたはこのようにして制御を参照してください可能性があります

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

はそれがお役に立てば幸いです。