Canvas with Path的isMouseOver

时间:2013-07-08 16:38:25

标签: c# wpf

抱歉我的英文。 我是WPF的新手,我有一个问题。 我希望在触发Canvas时更改包含路径(图片)的isMouseOver的视觉样式。

步骤:

  • 将画布放到窗口
  • 包含路径

结果:

<Canvas x:Name="mycanvas" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
         <Path Width="40" Height="30" Canvas.Left="18" Canvas.Top="23" Stretch="Fill" Fill="#FF000000" Data="F1 M 18,23L 58,23L 58,53L 18,53L 18,23 Z M 54,31L 22,31L 22,49L 54,49L 54,31 Z M 45,38L 45,42L 31,42L 31,38L 45,38 Z "/>
    </Canvas>

然后,如果出现Button,我将画布与isMouseOver样式和填充“路径”组合为白色。

我的风格:

<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type Button}">
    <Canvas x:Name="mycanvas" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
        <Path x:Name="path" Width="40" Canvas.Top="23" Stretch="Fill" Canvas.Left="18" Height="30" Fill="#FF000000" Data="F1 M 18,23L 58,23L 58,53L 18,53L 18,23 Z M 54,31L 22,31L 22,49L 54,49L 54,31 Z M 45,38L 45,42L 31,42L 31,38L 45,38 Z "/>
        </Canvas>
    <ControlTemplate.Triggers>
    <Trigger Property="IsFocused" Value="True"/>
    <Trigger Property="IsDefaulted" Value="True"/>
    <Trigger Property="IsMouseOver" Value="True" SourceName="mycanvas">
        <Setter Property="Fill" TargetName="path" Value="White"/>
    </Trigger>
    <Trigger Property="IsPressed" Value="True"/>
        <Trigger Property="IsEnabled" Value="False"/>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
</Style>

IsMouseOver仅在鼠标通过图像(“路径”)表面时才起作用。 但是图像表面很小并且包含断裂。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

Canvas Background 中添加SolidColorBrush Transparent 颜色:

 <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Canvas x:Name="mycanvas" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
                        <Canvas.Background>
                            <SolidColorBrush Color="Transparent"/>
                        </Canvas.Background>
                        <Path x:Name="path" Width="40" Canvas.Top="23" Stretch="Fill" Canvas.Left="18" Height="30" Fill="#FF000000" Data="F1 M 18,23L 58,23L 58,53L 18,53L 18,23 Z M 54,31L 22,31L 22,49L 54,49L 54,31 Z M 45,38L 45,42L 31,42L 31,38L 45,38 Z "/>
                    </Canvas>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True" SourceName="mycanvas">
                            <Setter Property="Fill" TargetName="path" Value="White"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>