Silverlight 3中的ControlTemplate.Triggers WPF等效项 [英] ControlTemplate.Triggers WPF equivalent in Silverlight 3

查看:83
本文介绍了Silverlight 3中的ControlTemplate.Triggers WPF等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF应用程序中有这个controltempalte +触发器内容.

I have this controltempalte + trigger stuff in my WPF application.

<ControlTemplate TargetType="me:MyControl" x:Key="fade">
    <ContentPresenter - other stuff />
    <ControlTemplate.Triggers>
        <Trigger Property="IsTransitioned" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation -<stuff>- />                                
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

这在WPF中很酷,对于我来说,根据上面的触发器编写代码也很直观.

This works cool in WPF, and is very intuitive too for me to write this based on trigger as above.

当我将其移植到Silverlight(3)时,被告知我必须使用VSM,状态和组等,因为不支持控制模板上的触发器. 我查看了一些示例,甚至没有尝试使用VSM位代替上面的触发器,但是无法使其正常工作.

When i port this to Silverlight (3), i am told i have to use VSM, states and groups etc. since triggers on control template is not supported. I h ave looked at some samples, and i even atempted to apply teh VSM bits in, in the place of trigger as above, but can't get it to work.

有人建议我,除了xaml中的VSM,我还必须处理一些事件等.

Someone suggested to me that apart from VSM in the xaml, i will have to handle some events etc.

SL3模型让我很痛苦.请帮忙.

SL3 model is just being painful for me. Please help.

推荐答案

Silverlight 3引入了交互触发器,据我所知,它可以做您想做的事情,但要复杂一些.到目前为止,关于它们的例子还很少.

Silverlight 3 introduced interaction triggers which as far as I can tell do what you want but are a little more complex. There's very few examples about them out yet though.

如果您手动执行此操作,则需要引用System.Windows.Interactivity和Microsoft.Expression.Interactions(从Blend 3中开始,如果已安装该类,则该类将在引用"选项卡中).

If you're doing this manually you need references to System.Windows.Interactivity and Microsoft.Expression.Interactions (from Blend 3, the class will be in your references tab if you've installed it).

如果在Blend中添加触发器,则它将自动添加触发器.这在Silverlight 3中称为行为",您可以在资产"选项卡的行为"部分的混合"中找到这些行为.

If you add the triggers in Blend then it will add those automatically. This is called Behaviours in Silverlight 3 and you will find these in Blend in the Behaviours section of the Assets tab.

它们如何工作的示例.请注意,故事板位于第二个矩形的资源中,我无法使其在ControlStoryboardAction.Storyboard内部工作,但是如果我将矩形设为ContentControl并将其放在Template中,则它确实可以工作.这可能是错误,或者我缺少某些东西:

An example of how they work. Note the storyboard sitting in the resource of the second rectangle, I couldn't get it to work inside the ControlStoryboardAction.Storyboard, but it did work if I made the rectangle a ContentControl and put it in the Template. This may be a bug or me missing something :

<UserControl
    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" 
    x:Class="SLTrigger.MainPage"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" 
    xmlns:im="clr-namespace:Microsoft.Expression.Interactivity.Media;assembly=Microsoft.Expression.Interactions">
  <Grid x:Name="LayoutRoot">
    <StackPanel>
        <Rectangle Margin="5" Fill="Blue" Width="200" Height="100">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonDown">
                    <ic:ChangePropertyAction PropertyName="Fill" Duration="0">
                        <ic:ChangePropertyAction.Value>
                            <SolidColorBrush Color="Red"/>
                        </ic:ChangePropertyAction.Value>
                    </ic:ChangePropertyAction>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Rectangle>
        <Rectangle Margin="5" x:Name="AnimatedRectangle2" Fill="Blue" Width="200" Height="100">
            <Rectangle.Resources>
                <Storyboard x:Key="AnimationStoryboard">
                    <ColorAnimation 
                        Storyboard.TargetName="AnimatedRectangle2"
                        Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                        To="Red"
                        AutoReverse="True"
                        Duration="0:0:0.5" />
                </Storyboard>
            </Rectangle.Resources>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonDown">
                    <im:ControlStoryboardAction ControlStoryboardOption="Play" Storyboard="{StaticResource AnimationStoryboard}">
                        <!--
                        Doesn't work, but does work inside control templates??
                        <im:ControlStoryboardAction.Storyboard>
                            <Storyboard>
                                <ColorAnimation 
                                        Storyboard.TargetName="AnimatedRectangle2"
                                        Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                        To="Red"
                                        AutoReverse="True"
                                        Duration="0:0:0.5" />
                            </Storyboard>
                        </im:ControlStoryboardAction.Storyboard>
                        -->
                    </im:ControlStoryboardAction>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Rectangle>
        <ContentControl>
            <ContentControl.Template>
                <ControlTemplate>
                    <Rectangle Margin="5" x:Name="AnimatedRectangle3" Fill="Blue" Width="200" Height="100">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseLeftButtonDown">
                                <im:ControlStoryboardAction ControlStoryboardOption="Play">
                                    <im:ControlStoryboardAction.Storyboard>
                                        <Storyboard>
                                            <ColorAnimation 
                                                    Storyboard.TargetName="AnimatedRectangle3"
                                                    Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                                    To="Red"
                                                    AutoReverse="True"
                                                    Duration="0:0:0.5" />
                                        </Storyboard>
                                    </im:ControlStoryboardAction.Storyboard>
                                </im:ControlStoryboardAction>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Rectangle>
                </ControlTemplate>
            </ContentControl.Template>
        </ContentControl>
        <TextBlock TextAlignment="Center" Text="Click the rectangles" />
      </StackPanel>
    </Grid>
</UserControl>

该类文件中没有任何内容:

The class file has nothing in it:

using System.Windows.Controls;

namespace SLTrigger
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }
}

这篇关于Silverlight 3中的ControlTemplate.Triggers WPF等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆