C# WPF-更改RepeatButton的DataTrigger中的ContentTemplate
我有以下问题,我需要一些帮助 (代码被简化以显示我遇到的问题) 我得到了一个带有内容模板和样式的重复按钮:C# WPF-更改RepeatButton的DataTrigger中的ContentTemplate,c#,.net,wpf,styling,repeatbutton,C#,.net,Wpf,Styling,Repeatbutton,我有以下问题,我需要一些帮助 (代码被简化以显示我遇到的问题) 我得到了一个带有内容模板和样式的重复按钮: <UserControl x:Class="SR.Testing.MyUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml
<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>
M0,5 L4.5,5 9,5 6,5 4.5,3.5 3,5 z
启动应用程序后,repeatbutton看起来与预期一致(darkorange):
但是,当我使用“IncreaseButton.IsEnabled=false;”禁用RepeatButton(通过codebehind)时
我希望我的样式触发器将箭头变为绿色:
但我得到的是这个(箭头保持橙色,背景变为白色/灰色):
造成这种行为的原因是什么?如何解决?谢谢 背景变为白色/灰色,因为基础样式中定义了ControlTemplate触发器。若要删除此选项,需要替代基础样式。但是你需要创建一个新的模板
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<!-- Template -->
</Setter.Value>
</Setter>
箭头保持橙色的原因是您直接在按钮上设置了模板。这将替代样式中的特性以及触发器。要解决此问题,只需添加(并删除按钮上的属性)
为你的风格干杯。谢谢,我设法让它工作起来了。但是我不得不使用这个链接:因为我使用的是重复按钮。是的,对不起,我的错误。没问题!再次感谢你的帮助。
<Setter Property="ContentTemplate" Value="{StaticResource ArrowUpNormal" />