用于覆盖事件上的鼠标的Windows.Resource

时间:2017-01-19 18:31:03

标签: c# wpf xaml

我已经看到了一些接近我的问题,也许我只是很难理解。我大约一周前开始使用WPF。我的程序根据一组文件动态添加按钮。一切都很好,除了我很难摆脱按钮的默认鼠标悬停事件。我在创建它们时给按钮图像背景,但是鼠标悬停在按钮上会使按钮变成蓝灰色。下面是我开始的一些代码,也许我在正确的轨道上?

由于

  <Window.Resources>
    <Button x:Key="MoviePosterStyle">
        <Style>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="False">                   
                </Trigger>                    
            </Style.Triggers>
        </Style>
    </Button>
</Window.Resources>

1 个答案:

答案 0 :(得分:0)

我认为您需要以如下样式制作按钮模板的本地副本:

<Style TargetType="Button">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ButtonBase}">
        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
          <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="Button.IsDefaulted" Value="True">
            <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
          </Trigger>
          <!--<Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Background" TargetName="border" Value="#FFBEE6FD"/>
          <Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/>
        </Trigger>-->
          <Trigger Property="IsPressed" Value="True">
            <Setter Property="Background" TargetName="border" Value="#FFC4E5F6"/>
            <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
          </Trigger>
          <Trigger Property="ToggleButton.IsChecked" Value="True">
            <Setter Property="Background" TargetName="border" Value="#FFBCDDEE"/>
            <Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/>
          </Trigger>
          <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
            <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

然后删除或取消注释IsMouseOver Trigger,如上所示。