2011-08-06 6 views
7

다음은 내 텍스트 블록입니다.WPF : 일부 컨트롤의 트리거에서 다른 컨트롤의 속성을 Chainging

<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은 Window 리소스에 있습니다.

참고 : 완벽하게 작동하는 스 니펫보다는 추적 할 때와 매우 흡사합니다! 몸에서

, 당신은 같은 방식으로 컨트롤을 참조 할 수 있습니다 :
<ContentControl Template="{StaticResource myCtl}" Content="this is my text block" /> 

는 도움이되기를 바랍니다.

관련 문제