【问题标题】:IsMouseOver triggers background color change only temporarilyIsMouseOver 仅临时触发背景颜色更改
【发布时间】:2012-12-13 14:33:33
【问题描述】:

我是这方面的初学者,并试图了解 WPF 和 XAML 的工作原理。下面的 sn-p 是来自 Nathans Unleashed 4.0 的书(一个微不足道的修改)。我将它插入到 Ok 按钮中:

<Button.Style>
 <Style TargetType=”{x:Type Button}”>
  <Style.Triggers>
   <Trigger Property=”IsMouseOver” Value=”True”>
    <Setter Property=”Background” Value=”Yellow”/>
   </Trigger>
  </Style.Triggers>
 </Style>
</Button.Style>

当我在 XAML crunsher 中运行它并将鼠标移到 Ok 按钮上时,该按钮确实将其背景颜色更改为黄色(很好),但立即将颜色重置为其原始值,即使鼠标停留在按钮上也是如此 - 为什么是这样?我希望它保持黄色,直到鼠标从按钮上移开。这是 XAML 粉碎机的问题,还是我的预期错误?

编辑(回复评论):这是完整的窗口,也是取自 Nathan 的书:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="About WPF Unleashed" SizeToContent="WidthAndHeight"
    Background="OrangeRed">
  <StackPanel>
   <Label FontWeight="Bold" FontSize="20" Foreground="White">
    WPF Unleashed (Version 3.0)
   </Label>
   <Label>© 2006 SAMS Publishing</Label>
   <Label>Installed Chapters:</Label>
   <ListBox>
    <ListBoxItem>Chapter 1</ListBoxItem>
    <ListBoxItem>Chapter 2</ListBoxItem>
   </ListBox>
   <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
   <Button MinWidth="75" Margin="10">Help</Button>
   <Button MinWidth="75" Margin="10">
    <Button.Style>
     <Style TargetType="{x:Type Button}">
      <Style.Triggers>
       <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Background" Value="Yellow"/>
       </Trigger>
      </Style.Triggers>
     </Style>
     </Button.Style>
     OK
     </Button>
    </StackPanel>
   <StatusBar>You have successfully registered this product.</StatusBar>
  </StackPanel>
</Window>

【问题讨论】:

  • 您的期望完全正确,我复制了您的代码,它确实按您的预期工作。
  • 按钮是否使用自定义模板?
  • @ErikÖjebo 不,据我所知,这只是一个普通按钮。更糟糕的是,如果我使用“前景”而不是“背景”,它会按预期工作(也就是说,前景色保持不变,直到鼠标离开按钮)。
  • 按钮中是否有不是标签或文本块的内容?如果该内容是 hittestvisible,它将接收鼠标悬停,而您的按钮不会。你能显示整个按钮代码吗?
  • 我相信正在发生的是按钮模板 VisualStateManager 覆盖了您的价值。你最好创建/修改一个 Button.Template 来做你想做的事。看看这个类似的SO Question

标签: wpf xaml triggers


【解决方案1】:

不幸的是,Button 内置了精美的悬停动画,您必须重写 ControlTemplate 才能阻止这种情况发生。

 <Button MinWidth="75" Margin="10" FocusVisualStyle="{x:Null}" Content="OK">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border Name="border" BorderThickness="1" Padding="4,2" BorderBrush="DarkGray" CornerRadius="3" Background="{TemplateBinding Background}">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Yellow"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

【讨论】:

    【解决方案2】:

    我将您的代码复制到一个新的 Visual Studio 2010 项目中并成功运行(.NET 4.0)。

    我认为问题是 XAML Cruncher 的错误。

    【讨论】:

      猜你喜欢
      • 2011-01-28
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2013-08-08
      • 2013-05-28
      相关资源
      最近更新 更多