相关疑难解决方法(0)

为什么Button的背景会改变?

我是WPF的初学者,我甚至不知道在哪里寻找这个问题的答案.这个XAML对我来说非常简单:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>
    <Button>
      <Button.Style>
        <Style TargetType="{x:Type Button}">
        <Style.Triggers>
          <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Green"/>
          </Trigger>      
        </Style.Triggers>
        </Style>
      </Button.Style>
      <Button.Content>Test</Button.Content>
    </Button>
  </Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)

当我将鼠标悬停在按钮上时,IsMouseOver更改为True触发器会使背景变为绿色.一瞬间.然后它变成蓝色.

更好的是:如果我将相同的setter附加到IsFocused属性,一旦我专注于按钮,背景颜色会在绿色和蓝色之间徘徊.

Button中有一些东西(我猜它是在Vista下使用的默认主题),这使得它以这种方式运行.我怀疑触发器需要设置另一个属性.但是什么?

wpf triggers styles button

4
推荐指数
2
解决办法
8746
查看次数

标签 统计

button ×1

styles ×1

triggers ×1

wpf ×1