【问题标题】:Change style on parent mouseover更改父鼠标悬停的样式
【发布时间】:2013-09-09 13:20:58
【问题描述】:

我为视图框做了一个触发器,当鼠标悬停在视图框上时,内容会发生变化。

这可行,但由于视图框的内容很小,我希望在视图框周围有一个边框,该边框也会触发鼠标悬停事件。

<Viewbox Width="50" x:Name="ActionIconBox1" Height="40.5">
    <ContentControl>
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                    <Setter Property="Content" Value="{StaticResource action_message}"/>
                    <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Viewbox,AncestorLevel=1}, Path=IsMouseOver}" Value="True" >
                                <Setter Property="Content" Value="{StaticResource action_message_focus}"/>
                            </DataTrigger>
                    </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
</Viewbox>

这仅在鼠标在内容控件上时有效,在鼠标进入 Viewbox 时无效。

资源是一个画布

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_message" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
<Path Width="38" Height="39.75" Canvas.Left="19" Canvas.Top="22" Stretch="Fill" Fill="#FF1BA1E2" Data="F1 M 33,51L 36.4167,61.75L 24,51L 19,51L 19,22L 57,22L 57,51L 33,51 Z "/> </Canvas>

为什么绑定不起作用?

【问题讨论】:

  • 究竟什么“不起作用”?另外,您是否尝试从Trigger 链接到appbar_message Canvas?你可能会遇到问题,因为你在Trigger 中将其称为action_message
  • 只有当鼠标在内容控件上时,内容才会改变,图像本身。但不是当鼠标在视图框上时。是的,不用担心只是复制粘贴错误。在我的代码中,链接是正确的。
  • viewbox 和 contentcontrol 之间有边距吗?

标签: c# wpf wpf-controls


【解决方案1】:

只需将其放入边框并更改 AncestorType。

    <Viewbox Width="50" x:Name="ActionIconBox1" Height="40.5" >
        <Border BorderThickness="2" BorderBrush="Red">
            <ContentControl>
                <ContentControl.Style>
                    <Style TargetType="{x:Type ContentControl}">
                        <Setter Property="Content" Value="{StaticResource action_message}"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Border,AncestorLevel=1}, Path=IsMouseOver}" Value="True" >
                                <Setter Property="Content" Value="{StaticResource action_message_focus}"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </Border>
    </Viewbox>

【讨论】:

    【解决方案2】:

    您将Trigger 放在ContentControl 上,所以它当然只适用于此。只需将ContentControl 的大小设置为与ViewBox 相同的大小即可删除您所称的“边框”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多