0

我有一个用于 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 属性上触发的控件模板,我试图将其更改为上述样式中的 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>

要清楚:

  1. 我想在 myProperty 上使用 DataTrigger
  2. 与当前的 IsChecked 操作具有相同的 Trigger 操作。(实施情节提要)

我尝试了很多不同的东西并搜索了很多帖子,但找不到我正在寻找的答案。

谁能告诉我我需要做什么?

4

2 回答 2

4

我想,您正在使用一种样式将其Template应用于ToggleButton. 如果没有,请创建一个默认样式ToggleButton并将Template属性设置为您的ControlTemplate

<Style TargetType="ToggleButton">
    <Setter Property="Template" Value="{StaticResource ClipBoardButton1}" />
</Style>

而且,关于使用 DataTriggers 的问题,我不会添加DataTriggersControlTemplate,因为,考虑一下这种情况,当您想在属性名称不同(不是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并将属性绑定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为:

<DataTrigger Binding="{Binding myProperty}" Value="True">
    <DataTrigger.EnterActions>
        <BeginStoryboard Storyboard="{StaticResource ButtonDownTimeLine}"/>
    </DataTrigger.EnterActions>
    <DataTrigger.ExitActions>
        <BeginStoryboard Storyboard="{StaticResource ButtonUpTimeLine}"/>
    </DataTrigger.ExitActions>
</DataTrigger>
于 2013-10-29T04:41:37.587 回答
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>
于 2013-10-29T01:57:59.673 回答