【问题标题】:WPF : Chainging another control's property in some control's TriggerWPF:在某个控件的触发器中链接另一个控件的属性
【发布时间】:2011-10-21 11:53:00
【问题描述】:

这是我的文本块。

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

我该怎么做?

【问题讨论】:

  • 那张图片(或您的意思的控件)在哪里?
  • 基本上,我想在同一个窗口中更改另一个控件的属性。而且,控件也可以放在应用程序资源、窗口资源、控件资源中。

标签: wpf xaml triggers styles


【解决方案1】:

您必须以某种方式聚合源和目标。

您可以创建包含超链接/文本块和图像的自定义控件。如果您有多个在此类示例中运行的块,则这是首选方式。

如果你不喜欢这个。您可以按如下方式创建“临时”匿名控件:

<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 资源中。

注意:它更像是一个跟踪,而不是一个功能齐全的 sn-p!

在body中,你可以这样引用控件:

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-03
    • 2011-02-01
    • 1970-01-01
    • 2012-12-13
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    相关资源
    最近更新 更多