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

标签 wpf xaml triggers styles

这是我的文本块。

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

希望能帮助到你。

关于WPF : Chainging another control's property in some control's Trigger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6967367/

相关文章:

带有 ItemsSource 的 WPF ContextMenu - 如何在每个项目中绑定(bind)到 Command?

c# - 如何通过FindWindow api调用C#开发的window

wpf - 有没有办法重新使用已经关闭的 WPF 窗口实例

wpf - 从 ItemTemplate 中绑定(bind)到 ItemsControl 的 DataContext

sql - SQL Server 中丢失触发器

c# - 将 WPF DataGrid 绑定(bind)到 List<Interface>

WPF 附加属性在 xaml 中设置时不会触发代码

c# - 如何将此语句转换为XAML?

mysql - 触发器和 IF 为 NULL

sql - mysql语法问题