簡體   English   中英

WPF:在某個控件的Trigger中追逐另一個控件的屬性

[英]WPF : Chainging another control's property in some control's Trigger

這是我的文本塊。

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

我喜歡制作一個xaml代碼,它可以改變另一個控件的功能,比如“imgAnother”。

我怎樣才能做到這一點?

您必須以某種方式聚合源和目標。

您可以創建包含超鏈接/文本塊和圖像的自定義控件。 如果您有幾個塊在這樣的示例中表現,那么這是首選方法。

如果你不喜歡這個。 您可以創建一個“臨時”匿名控件,如下所示:

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

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM