TargetedTriggerAction - 无法找到目标项目

时间:2011-03-29 18:44:15

标签: silverlight xaml

我正在尝试将触发器连接到更改图像的RenderTransform的路径。当我运行我的行为代码时,Target为null。我不确定我做错了什么?

这是我的行为:

public class CountryZoomBehaviorTarget : TargetedTriggerAction<Image>
{

    private Image _targetedImage { get; set; }

    protected override void OnAttached()
    {
        base.OnAttached();
        _targetedImage = this.Target;

        if (null == _targetedImage)
            return;
        else
        {
            _targetedImage.MouseEnter += new MouseEventHandler(AssociatedObject_MouseEnter2);
            _targetedImage.MouseLeave += new MouseEventHandler(AssociatedObject_MouseLeave2);

        }

    }
    // Remove transform from object.
    void AssociatedObject_MouseLeave2(object sender, MouseEventArgs e)
    {

        ScaleTransform temp = new ScaleTransform();
        _targetedImage.RenderTransform = null;
    }

    // Grow the transform and attach transform to object
    void AssociatedObject_MouseEnter2(object sender, MouseEventArgs e)
    {

        ScaleTransform temp = new ScaleTransform();
        temp.ScaleX = 1.10;
        temp.ScaleY = 1.10;
        _targetedImage.RenderTransformOrigin = new Point(0.5, 0.5);

        _targetedImage.RenderTransform = temp;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        _targetedImage.MouseEnter -= AssociatedObject_MouseEnter2;
        _targetedImage.MouseLeave -= AssociatedObject_MouseLeave2;
    }


    protected override void Invoke(object parameter)
    {
        throw new NotImplementedException();
    }
}

这是我的XAML:

                    <Image Grid.Column="1" Margin="8,49,0,163" Source="Images/NorthAmer.png" Stretch="Fill" HorizontalAlignment="Left" Width="364" x:Name="NorthAmerica"/>
                <Path Grid.Column="1" Data="M65,155 L11.5,175.5 L39.5,177.5 L84,146.5 L107.5,165.5 L120,211 L113.5,245 L135.5,290 L153.5,303.5 L176.5,312 L209,331.5 L212,327.5 L189.5,306.5 L188,295 L173.5,304.5 L164.5,288.5 L172.5,274.5 L200,276 L205,287 L209.5,265.5 L223.5,254 C223.5,254 252,231 254,229.5 C256,228 259.5,181 259.5,181 L245.5,169 L264.5,166 L271.5,155.5 L253,109 L265,89.5 L278.5,102 L290,106 L294,177 L303,183.5 L312.5,162.5 L347.5,144 L359.5,94 L372.5,77.5 L340,57.5 L308,62.5 L282.5,48 L193,75 L193.5,90.5 L186,94.5 L177.5,90 L167.5,102.5 L176.5,124 L149.5,109 L137,111 L106,86.5 L79,97 L65.5,117.5 L56,141 z" Margin="11.5,48,413.5,162.5" Stretch="Fill" UseLayoutRounding="False" d:LayoutOverrides="GridBox">
                    <interaction:Interaction.Triggers>
                        <interaction:EventTrigger EventName="KeyDown" >
                    <vm:CountryZoomBehaviorTarget TargetName="NorthAmerica" />
                        </interaction:EventTrigger>
                    </interaction:Interaction.Triggers>
                </Path>

1 个答案:

答案 0 :(得分:0)

我能够让这个工作。只能在加载关联对象时访问目标对象。

这是一个很好的参考:http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx