首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF -在RepeatButton的DataTrigger中更改ContentTemplate

WPF -在RepeatButton的DataTrigger中更改ContentTemplate
EN

Stack Overflow用户
提问于 2020-06-05 22:57:31
回答 1查看 61关注 0票数 0

我遇到了以下问题,需要一些帮助。

(代码被简化以显示我遇到的问题)

我有一个带有内容模板和样式的重复按钮:

代码语言:javascript
复制
<UserControl x:Class="SR.Testing.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">


  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Border x:Name="Border" SnapsToDevicePixels="True">
      <StackPanel>
        <RepeatButton x:Name="IncreaseButton" ContentTemplate="{StaticResource ArrowUpNormal}" Style="{StaticResource IncreaseRepeatButtonStyle}" Click="IncreaseClick" />
      </StackPanel>
    </Border>
  </Grid>
</UserControl>

这是数据模板和样式:

代码语言:javascript
复制
  <Geometry x:Key="UpArrowGeometry">M0,5 L4.5,.5 9,5 6,5 4.5,3.5 3,5 z</Geometry>

  <DataTemplate x:Key="ArrowUpNormal">
    <Path Width="18"
          Height="10"
          Stretch="Fill"
          Data="{StaticResource UpArrowGeometry}"
          Fill="DarkOrange"
          SnapsToDevicePixels="True"
          HorizontalAlignment="Center"
          VerticalAlignment="Center"
          Focusable="False" />
  </DataTemplate>

  <DataTemplate x:Key="ArrowUpDisabled">
    <Path Width="18"
          Height="10"
          Stretch="Fill"
          Data="{StaticResource UpArrowGeometry}"
          Fill="Green"
          SnapsToDevicePixels="True"
          HorizontalAlignment="Center"
          VerticalAlignment="Center"
          Focusable="False" />
  </DataTemplate>

  <Style x:Key="IncreaseRepeatButtonStyle" TargetType="{x:Type RepeatButton}">
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Height" Value="40" />

    <Style.Triggers>
      <Trigger Property="IsEnabled" Value="False">
        <Setter Property="ContentTemplate" Value="{StaticResource ArrowUpDisabled}" />
      </Trigger>
    </Style.Triggers>
  </Style>

启动应用程序后,repeatbutton按钮显示为预期效果(深橙色):

但是,当我使用"IncreaseButton.IsEnabled = false;“禁用RepeatButton (通过代码隐藏)时;

我希望我的风格触发器能把箭头变成绿色:

但是我得到了这样的结果(箭头保持橙色,背景变成白色/灰色):

是什么导致了这种行为?我该如何解决这个问题?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-05 23:12:20

背景会变成白色/灰色,因为在基本样式中定义了ControlTemplate触发器。要删除它,您需要覆盖基本样式。但是接下来你需要创建一个新的模板。

代码语言:javascript
复制
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
    <Setter.Value>
        <!--  Template  -->
    </Setter.Value>
</Setter>

Link to the base style with template

箭头保持橙色的原因是您已经直接在按钮上设置了模板。这将覆盖样式中的属性以及触发器。要解决这个问题,只需添加(并删除按钮上的属性)

代码语言:javascript
复制
<Setter Property="ContentTemplate" Value="{StaticResource ArrowUpNormal" />

你的风格。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62218311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档