我有一个用于ToggleButton的大型ControlTemplate。我试图添加一个没有成功的数据触发器。
但我已经使用样式添加它,如下所示:
<Window.Resources>
<Style x:Key="toggleBtnStyle" TargetType="{x:Type ToggleButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=myProperty}" Value="true">
<Setter Property="Content" Value="IS TRUE"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=myProperty}" Value="false">
<Setter Property="Content" Value="IS FALSE"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<ToggleButton Height="34" HorizontalAlignment="Left" x:Name="toggleBroadcast" Click="Button_Click" VerticalAlignment="Top" Width="133" Margin="180,0,0,0" Style="{DynamicResource toggleBtnStyle}"/>
有没有办法将其添加到控制模板?我有以下控件模板,它是在ToggleButton的IsChecked属性上触发的,我试图将它更改为上面Style中的DataTrigger,但没有成功。
<ControlTemplate x:Key="ClipBoardButton1" TargetType="{x:Type ToggleButton}">
<Border BorderThickness="1,1,1,1" CornerRadius="1,1,1,1" BorderBrush="Black" Background="Black" >
<Grid>
<Border x:Name="BorderUp" BorderThickness="1,1,1,1" CornerRadius="1,1,1,1" Background="Blue"/>
<Border x:Name="BorderDown" BorderThickness="1,1,1,1" CornerRadius="1,1,1,1" Opacity="0" Background="Aqua"/>
<ContentPresenter x:Name="Contents" HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Margin="0,0,0,0"/>
</Grid>
</Border>
<ControlTemplate.Resources>
<Storyboard x:Key="ButtonDownTimeLine">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="BorderDown" Storyboard.TargetProperty="Opacity">
<SplineDoubleKeyFrame KeyTime="00:00:00.05" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Contents" Storyboard.TargetProperty="Margin">
<SplineThicknessKeyFrame KeyTime="00:00:00.025" Value="0.5,0.5,0,0"/>
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="ButtonUpTimeLine">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="BorderDown" Storyboard.TargetProperty="Opacity">
<SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Contents" Storyboard.TargetProperty="Margin">
<SplineThicknessKeyFrame KeyTime="00:00:00.25" Value="0,0,0,0"/>
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource ButtonDownTimeLine}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource ButtonUpTimeLine}"/>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
要明确:
我尝试了很多不同的东西并搜索了很多帖子,但无法找到我想要的答案。
谁能告诉我我需要做什么?
答案 0 :(得分:4)
我想,您正在使用 样式将此Template
应用于ToggleButton
。如果没有,请为ToggleButton
创建默认样式,并将Template
属性设置为ControlTemplate
<Style TargetType="ToggleButton">
<Setter Property="Template" Value="{StaticResource ClipBoardButton1}" />
</Style>
而且,围绕使用DataTriggers遇到问题,我不会将DataTriggers
添加到ControlTemplate
,因为,想想这个场景,当你想在不同的地方使用同一个模板时会发生什么属性名称不同的地方(不是myProperty
)?
要实现您的目标,您可以将Trigger
中的ControlTemplate
更新为以下内容:
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="IS TRUE" />
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource ButtonDownTimeLine}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource ButtonUpTimeLine}"/>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Content" Value="IS FALSE"></Setter>
</Trigger>
并将IsChecked
的{{1}}属性绑定到您的ToggleButton
myProperty
<强>更新强>
将<ToggleButton Height="34" HorizontalAlignment="Left"
x:Name="toggleBroadcast"
Click="Button_Click"
VerticalAlignment="Top"
Width="133" Margin="180,0,0,0"
IsChecked="{Binding myProperty}"/>
中的触发器替换为:
ControlTemplate
答案 1 :(得分:0)
您可以在样式中使用控件模板。如下。我希望这有帮助
<Window.Resources>
<Style x:Key="toggleBtnStyle" TargetType="{x:Type ToggleButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=myProperty}" Value="true">
<Setter Property="Content" Value="IS TRUE"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=myProperty}" Value="false">
<Setter Property="Content" Value="IS FALSE"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border BorderThickness="1,1,1,1" CornerRadius="1,1,1,1" BorderBrush="Black" Background="Black" >
<Grid>
<Border x:Name="BorderUp" BorderThickness="1,1,1,1" CornerRadius="1,1,1,1" Background="Blue"/>
<Border x:Name="BorderDown" BorderThickness="1,1,1,1" CornerRadius="1,1,1,1" Opacity="0" Background="Aqua"/>
<ContentPresenter x:Name="Contents" HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Margin="0,0,0,0"/>
</Grid>
</Border>
<ControlTemplate.Resources>
<Storyboard x:Key="ButtonDownTimeLine">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="BorderDown" Storyboard.TargetProperty="Opacity">
<SplineDoubleKeyFrame KeyTime="00:00:00.05" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Contents" Storyboard.TargetProperty="Margin">
<SplineThicknessKeyFrame KeyTime="00:00:00.025" Value="0.5,0.5,0,0"/>
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="ButtonUpTimeLine">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="BorderDown" Storyboard.TargetProperty="Opacity">
<SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Contents" Storyboard.TargetProperty="Margin">
<SplineThicknessKeyFrame KeyTime="00:00:00.25" Value="0,0,0,0"/>
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource ButtonDownTimeLine}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource ButtonUpTimeLine}"/>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>