如何在鼠标悬停按钮WPF(c#,wpf)时更改椭圆的颜色

时间:2015-01-28 09:12:56

标签: c# wpf

我在按钮上添加了一个椭圆。 但截至目前,鼠标悬停在该按钮上我需要更改背景。 所以colould无法正常触发,即在鼠标悬停事件上设置背景属性。

    <Style x:Key="RButton" TargetType="Button">
            <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>                          
                        <Ellipse x:Name="Elipse1" Fill="Red" ></Ellipse>
                        <ContentPresenter  HorizontalAlignment="Center"                VerticalAlignment="Center"></ContentPresenter>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
            </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value = "Orange"/>
                </Trigger>
        </Style.Triggers>
    </Style>

2 个答案:

答案 0 :(得分:0)

您可以在模板内使用TemplateBinding绑定Button.Background属性,并将默认值设置为Setter

的另一个Style
<Style x:Key="RButton" TargetType="Button">
   <Setter Property="Background" Value="Red"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="Button">
            <Grid>                          
               <Ellipse x:Name="Elipse1" Fill="{TemplateBinding Background}" />
               <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
   <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
         <Setter Property="Background" Value="Orange"/>
      </Trigger>
   </Style.Triggers>
</Style>

或者,如果您不想使用Button.Background属性,则可以按名称定位Ellipse

<Setter TargetName="Elipse1" Property="Background" Value="Orange"/>

答案 1 :(得分:0)

试试这个Style

 <Style x:Key="RButton" TargetType="Button">
     <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="Button">
                 <Grid>
                     <Ellipse x:Name="Elipse1" Fill="Red" ></Ellipse>
                     <ContentPresenter  HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                 </Grid>
                 <ControlTemplate.Triggers>
                     <Trigger Property="IsMouseOver" Value="True">
                         <Setter Property="Fill" Value = "Orange" TargetName="Elipse1" ></Setter>
                     </Trigger>
                 </ControlTemplate.Triggers>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
 </Style>