代码之家  ›  专栏  ›  技术社区  ›  Robby Smet

更改父鼠标悬停的样式

  •  0
  • Robby Smet  · 技术社区  · 13 年前

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

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

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

    为什么装订不起作用?

    2 回复  |  直到 13 年前
        1
  •  1
  •   Chevul Ervin    13 年前

    只需将其放入边界并更改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
  •  1
  •   Sheridan    13 年前

    你把 Trigger 上 ContentControl 所以它当然只在这方面起作用。只需设置 内容控制 大小与 ViewBox 删除你所说的“边界”。