在mouseover上更改stackpanel子项的样式

时间:2014-12-18 15:34:35

标签: c# wpf

如何在鼠标悬停时在堆叠面板中设置2个不同控件的样式? 我有以下内容:

<StackPanel Orientation="Horizontal">
      <Image x:Name="InfoImg" Source="off.png"/>
      <Label x:Name="InfoLabel" Content="OFF"/>
</StackPanel>

onMouseOver stackpanel,我想将图像源更改为

<Image x:Name="InfoImg" Source="on.png"/>

和标签文字

<Label x:Name="InfoLabel" Content="ON"/>

提前致谢

1 个答案:

答案 0 :(得分:1)

<StackPanel>
    <StackPanel.Style>
        <Style TargetType="{x:Type StackPanel}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="YOUR_IMAGE_NAME" Property="Source" Value="on.png"/>
                    <Setter TargetName="YOUR_TEXTBLOCK_NAME" Property="Text" Value="ON"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    <StackPanel.Resources>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Source" Value="off.png"/>
        </Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Text" Value="OFF"/>
        </Style>
    </StackPanel.Resources>
</StackPanel>

如果您只想将这些样式应用于特定的图像&amp; TextBlocks等。

相关问题