Fork me on GitHub

Silverlight模拟鼠标双击事件

DoubleClickHelper.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.Threading;

namespace SGSGHome.Views.Sys
{
    /// <summary>
    /// Double Click Event
    /// </summary>
    public class DoubleClickHelper
    {
        private DispatcherTimer timer;
        private FrameworkElement currentFrameworkElement;
        public event EventHandler<DoubleClickEventArgs> OnDoubleClick;

        public DoubleClickHelper()
        {
            if (timer == null)
            {
                timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromMilliseconds(300);
                timer.Tick += new EventHandler(timer_Tick);
            }
        }

        public void SendMessage(FrameworkElement fe)
        {
            if (fe == null)
            {
                return;
            }

            if (timer.IsEnabled == false)
            {
                timer.Start();
            }

            if (!fe.Equals(currentFrameworkElement))
            {
                fe.Tag = 1;
            }
            else
            {
                fe.Tag = 2;
            }

            currentFrameworkElement = fe;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (timer.IsEnabled == true)
            {
                timer.Stop();
            }

            if ((int)currentFrameworkElement.Tag == 2)
            {
                OnDoubleClick(null, new DoubleClickEventArgs(currentFrameworkElement));
            }

            currentFrameworkElement = null;
        }
    }

    public class DoubleClickEventArgs : EventArgs
    {
        private FrameworkElement currentFrameworkElement;

        public FrameworkElement CurrentFrameworkElement
        {
            get { return currentFrameworkElement; }
            set { currentFrameworkElement = value; }
        }

        public DoubleClickEventArgs(FrameworkElement fe)
        {
            currentFrameworkElement = fe;
        }
    }

    public static class UIElementExtensions
    {
        public static T ParentOfType<T>(this UIElement element) where T : DependencyObject
        {
            if (element == null)
            {
                return default(T);
            }
            DependencyObject parent = VisualTreeHelper.GetParent(element);
            while ((parent != null) && !(parent is T))
            {
                parent = VisualTreeHelper.GetParent(parent);
            }
            return (T)parent;
        }
    }
}
UserInRole.xaml
<controls:ChildWindow x:Class="SGSGHome.Views.Sys.UserInRole"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="500" Height="400" 
           Title="角色用户">
    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ListBox Name="lbUserNotInRole" Grid.Row="0" Grid.Column="0" Margin="2" SelectionMode="Single" MouseLeftButtonUp="lbUserNotInRole_MouseLeftButtonUp"></ListBox>
        <ListBox Name="lbUserInRole" Grid.Row="0" Grid.Column="1" Margin="2" SelectionMode="Single" MouseLeftButtonUp="lbUserInRole_MouseLeftButtonUp"></ListBox>
        <Button x:Name="CancelButton" Content="取消" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" Grid.ColumnSpan="2" />
        <Button x:Name="OKButton" Content="确定" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" Grid.ColumnSpan="2" />
    </Grid>
</controls:ChildWindow>
UserInRole.xaml.cs
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;

namespace SGSGHome.Views.Sys
{
    public partial class UserInRole : ChildWindow
    {
        CoreService.CoreServiceClient client;
        public long RoleID { get; set; }
        DoubleClickHelper lbUserNotInRoleEvent;
        DoubleClickHelper lbUserInRoleEvent;
        public UserInRole()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(UserInRole_Loaded);
        }

        void UserInRole_Loaded(object sender, RoutedEventArgs e)
        {
            //自定义双击事件
            lbUserInRoleEvent = new DoubleClickHelper();
            lbUserNotInRoleEvent = new DoubleClickHelper();
            lbUserInRoleEvent.OnDoubleClick += new EventHandler<DoubleClickEventArgs>(lbUserInRoleEvent_OnDoubleClick);
            lbUserNotInRoleEvent.OnDoubleClick += new EventHandler<DoubleClickEventArgs>(lbUserNotInRoleEvent_OnDoubleClick);

            client = new CoreService.CoreServiceClient();
            client.GetUserInRoleCompleted += new EventHandler<CoreService.GetUserInRoleCompletedEventArgs>(client_GetUserInRoleCompleted);
            client.GetUserNotInRoleCompleted += new EventHandler<CoreService.GetUserNotInRoleCompletedEventArgs>(client_GetUserNotInRoleCompleted);
            client.AddUserToRoleCompleted += new EventHandler<CoreService.AddUserToRoleCompletedEventArgs>(client_AddUserToRoleCompleted);
            client.DeleteUserFromRoleCompleted += new EventHandler<CoreService.DeleteUserFromRoleCompletedEventArgs>(client_DeleteUserFromRoleCompleted);

            client.GetUserInRoleAsync(this.RoleID);
            client.GetUserNotInRoleAsync(this.RoleID);
        }

        void lbUserNotInRoleEvent_OnDoubleClick(object sender, DoubleClickEventArgs e)
        {
            var clickItem = e.CurrentFrameworkElement as ListBoxItem;
            var dataItem = clickItem.Content as CoreService.sp_UserNotInRole_Result;

            client.AddUserToRoleAsync(dataItem.SamAccountName, this.RoleID);
        }

        void lbUserInRoleEvent_OnDoubleClick(object sender, DoubleClickEventArgs e)
        {
            var clickItem = e.CurrentFrameworkElement as ListBoxItem;
            var dataItem = clickItem.Content as CoreService.sp_UserInRole_Result;

            client.DeleteUserFromRoleAsync(dataItem.SamAccountName, this.RoleID);
        }

        void client_DeleteUserFromRoleCompleted(object sender, CoreService.DeleteUserFromRoleCompletedEventArgs e)
        {
            client.GetUserInRoleAsync(this.RoleID);
            client.GetUserNotInRoleAsync(this.RoleID);
        }

        void client_AddUserToRoleCompleted(object sender, CoreService.AddUserToRoleCompletedEventArgs e)
        {
            client.GetUserInRoleAsync(this.RoleID);
            client.GetUserNotInRoleAsync(this.RoleID);
        }

        void client_GetUserNotInRoleCompleted(object sender, CoreService.GetUserNotInRoleCompletedEventArgs e)
        {
            lbUserNotInRole.ItemsSource = e.Result;
            lbUserNotInRole.DisplayMemberPath = "DisplayName";
            lbUserNotInRole.SelectedValuePath = "SamAccountName";
        }

        void client_GetUserInRoleCompleted(object sender, CoreService.GetUserInRoleCompletedEventArgs e)
        {
            lbUserInRole.ItemsSource = e.Result;
            lbUserInRole.DisplayMemberPath = "DisplayName";
            lbUserInRole.SelectedValuePath = "SamAccountName";
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void lbUserNotInRole_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //添加用户到角色
            var fe = e.OriginalSource as FrameworkElement;
            lbUserNotInRoleEvent.SendMessage(fe.ParentOfType<ListBoxItem>());
        }

        private void lbUserInRole_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //从角色移除用户
            var fe = e.OriginalSource as FrameworkElement;
            lbUserInRoleEvent.SendMessage(fe.ParentOfType<ListBoxItem>());
        }
    }
}

 

posted @ 2012-11-23 15:51  Nick.Chung  阅读(251)  评论(0编辑  收藏  举报