1

对不起我的英语不好。我是 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仅当鼠标穿过图像(“路径”)表面时才有效。但是图像表面很小并且包含中断。我怎么解决这个问题?

4

1 回答 1

3

Canvas BackgroundSolidColorBrush一个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>
于 2013-07-08T17:07:23.860 回答