WPF - 更改RepeatButton的DataTrigger中的ContentTemplate。

问题描述 投票:0回答:1

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

(代码是简化的,以显示我所遇到的问题)

我得到了一个带有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>

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

然而,当我用 "AddressButton.IsEnabled = false; "禁用RepeatButton时(通过codebehind)。

我期望我的样式触发器能把箭头变成绿色。

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

是什么原因导致了这种行为,我该如何解决?谢谢!我遇到了以下问题,需要解决。

c# .net wpf styling repeatbutton
1个回答
1
投票

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

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

用模板链接到基础样式

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

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

适应你的风格。

© www.soinside.com 2019 - 2024. All rights reserved.