DataTrigger

时间:2016-04-28 15:27:59

标签: wpf xaml animation datatrigger

我有一个Template按钮,如下所示

<Button>            
        <Button.Style>                        
            <Style TargetType="{x:Type Button}">                            
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border BorderBrush="Transparent">
                                <Path Stretch="Fill" Width="25" Height="25" Fill="{TemplateBinding Background}" >
                                   <Path.Data>
                                      <RectangleGeometry Rect="10,10 200,140"/>
                                   </Path.Data>
                                </Path>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>
    </Button>        

我正在尝试为Background DataTrigger中的Style设置动画,如下所示,

 <Style.Triggers>
     <DataTrigger Binding="{Binding IsNoteOpen}" Value="true">
         <DataTrigger.EnterActions>
              <BeginStoryboard x:Name="Sb1">
                  <BeginStoryboard.Storyboard>
                      <Storyboard>
                         <ColorAnimation 
                             Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" 
                             From="White" To="Red" Duration="0:00:0.3" AutoReverse="True"/>
                       </Storyboard>
                   </BeginStoryboard.Storyboard>
               </BeginStoryboard>
            </DataTrigger.EnterActions>
          <DataTrigger.ExitActions>
              <StopStoryboard BeginStoryboardName="Sb1"/>
          </DataTrigger.ExitActions>
      </DataTrigger>
   </Style.Triggers>

但它没有在DataTrigger上动画。任何帮助

1 个答案:

答案 0 :(得分:0)

以下是我所做的事情:

form

我已将[HttpPost] public ActionResult Mymethod(string name) { ViewBag.Message = "Hello what is ur name ???"; ViewBag.Name = name; return View(); } 纳入其中并对其进行了测试。我可以确认这是在我的电脑上运行 顺便说一句,这是part_ButtonBorder:

<Style TargetType="Button" x:Key="MainTilesButton">
        <Setter Property="Background" Value="#CCF5F5F5"/><!--This is background colour with opcaity defined-->
        <Setter Property="Margin" Value="20"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="border" Background="{TemplateBinding Background}" Style="{StaticResource part_ButtonBorder}">
                        <Border.RenderTransform>
                            <RotateTransform/>
                        </Border.RenderTransform>
                        <ContentPresenter VerticalAlignment="Center" 
                            HorizontalAlignment="Center" 
                            TextBlock.FontFamily="Segoe UI Semibold"
                            TextBlock.TextAlignment="Justify" 
                            TextBlock.FontSize="26.667"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="MouseEnter">
                            <BeginStoryboard>
                                <Storyboard>
                                    <!--<DoubleAnimation Storyboard.TargetProperty="(Rectangle.RenderTransform).(RotateTransform.Angle)" To="-360" Duration="0:0:1" RepeatBehavior="Forever"/>-->
                                    <ThicknessAnimation Storyboard.TargetProperty="Margin" To="10" Duration="0:0:0.2"/>
                                        <ColorAnimation 
                                             Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" 
                                             From="White" To="ForestGreen" Duration="0:00:0.3" AutoReverse="True"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseLeave">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ThicknessAnimation Storyboard.TargetProperty="Margin" To="20" Duration="0:0:0.2"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="border" Property="Background" Value="Red"/>
                        </Trigger>
                        <!--<EventTrigger RoutedEvent="Loaded">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ThicknessAnimation Storyboard.TargetProperty="Margin" To="20" Duration="0:0:0.2"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>-->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>  

编辑
我已尝试在我的xaml中使用路径并且它按预期工作,路径填充设置为按钮的背景,您可能看不到它的原因是因为路径颜色将与按钮的背景相同。
这是包含路径的代码片段,注意路径已定义笔划,因此您可以看到它的轮廓。

Storyboard

需要注意的另一件事就是Path it self,我注意到我使用的那个位置非常低,所以当你想看到它时你需要伸展它窗口稍微一点,你就可以看到它。