分享

Silverlight之自定义Behavior

 Jcstone 2013-04-28

Silverlight之自定义Behavior

时间:2012-09-27 18:35来源:博客园 作者:sb001 点击:
首先,请确保项目同时引用了 Microsoft.Expression.Interactions 和 System.Windows.Interactivity DLL。 自定义Behavior文件: MyBehavior.cs using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink;
  

  首先,请确保项目同时引用了 Microsoft.Expression.Interactions 和 System.Windows.Interactivity DLL。

  自定义Behavior文件:

  MyBehavior.cs

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace behaviorTest
{
    public class MyBehavior:Behavior<TextBlock>
    {
        public MyBehavior()
        {
 
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown);
        }

        void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show(AssociatedObject.Text.ToString());
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.MouseLeftButtonDown-=new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown);
        }

    }
}

  前台页面:

<UserControl x:Class="behaviorTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas./markup-compatibility/2006"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:my="clr-namespace:behaviorTest"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name="tb" Text="HelloWorld" HorizontalAlignment="Center" VerticalAlignment="Center">
            <!--<i:Interaction.Behaviors>
                <my:MyBehavior />
            </i:Interaction.Behaviors>-->
        </TextBlock>
    </Grid>
</UserControl>

  如果在后台加入Behavior的话,请看代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using Microsoft.Expression.Interactivity.Layout;

namespace behaviorTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            //自带的拖动行为
            //Interaction.GetBehaviors(tb).Add(new MouseDragElementBehavior());
            //自己定义的行为
            Interaction.GetBehaviors(tb).Add(new MyBehavior());
        }
    }
}

  本文来自sb001的博客,原文地址:http://www.cnblogs.com/caok168/archive/2012/09/22/2698062.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多