WPF - 在 RepeatButton 的 DataTrigger 中更改 ContentTemplate

大夫

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

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

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

<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>

这是数据模板和样式:

  <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>

启动应用程序后,重复按钮看起来像预期的一样(深橙色):

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

我希望我的样式触发器将箭头变为绿色:

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

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

尼克拉斯

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

<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
    <Setter.Value>
        <!--  Template  -->
    </Setter.Value>
</Setter>

使用模板链接到基本样式

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

<Setter Property="ContentTemplate" Value="{StaticResource ArrowUpNormal" />

到你的风格。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

WPF:TabControl.ItemTemplate和TabItem.ContentTemplate之间的区别

如何通过WPF DataTrigger在多值转换器中传递多个参数

WPF-在DataTrigger上更改按钮模板化的文本颜色

WPF ListView对象DataTrigger

WPF如何将DataTrigger应用于XAML中的“自定义用户控件”类

WPF DataGrid AlternatingRowBackground覆盖背景的Style DataTrigger

根据DataTrigger更改Material WPF复选框的边框颜色

C#WPF基于DataTrigger更改ContentControl内容不起作用

WPF ValidationRule和DataTrigger禁用“提交”按钮

具有按下的VisualState的WPF RepeatButton

从DataTrigger在WPF TreeViewItem上设置IsExpanded

DataTrigger不会更改ListBox的ItemContainerStyle中的模板

WPF DataTrigger设置“宽度窗口”属性

DataTrigger WPF上的BeginStorybard

DataTrigger中的背景颜色未更改

WPF RepeatButton内容对齐?

具有textBlock contentTemplate的WPF按钮样式,如何更改textblock前景色情节提要

WPF通过触发器更改UserControl中的ContentTemplate

在WPF中将对象绑定到DataTrigger

WPF DataTrigger可以停用键绑定吗?

WPF DataTrigger在ComboBox上不起作用?

WPF中的DataTrigger问题

无法解析Datatrigger WPF中的绑定

基于变量的WPF Datagrid DataTrigger

WPF-从DataTrigger更新依赖项属性

Wpf 绑定 + DataTrigger

如何在 WPF 中的 Validation.ErrorTemplate 中进行 DataTrigger 绑定

WPF 为 TabItem ContentTemplate 动态设置上下文

如何使用 DataTrigger 更改 ItemsControl 中的 ItemsPanel?