locked
Silverlight 4 是否支持双击事件? RRS feed

  • 问题

  • Silverlight 4 是否支持双击事件?如果不支持,可有解决方案?
    2010年5月20日 1:36

答案

  • 不支持

    <UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" x:Class="DoubleClickTest.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.openxmlformats.org/markup-compatibility/2006" 
      mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
      <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Border BorderBrush="#FF000000" CornerRadius="5" BorderThickness="2" Background="#FFFFFFFF" Grid.Row="0" Margin="5">
          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition></ColumnDefinition>
              <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
    
            <controls:TreeView x:Name="TreeView1" HorizontalAlignment="Left"
              Margin="10" VerticalAlignment="Top"            
                    MouseLeftButtonUp="TreeViewItem_MouseLeftButtonUp">
              <controls:TreeViewItem Header="Personal Folders" IsExpanded="True">
                <controls:TreeViewItem Header="Deleted Items"/>
                <controls:TreeViewItem Header="Drafts"/>
                <controls:TreeViewItem Header="Inbox" IsExpanded="True">
                  <controls:TreeViewItem Header="Folders" />
                </controls:TreeViewItem>
                <controls:TreeViewItem Header="Junk E-mails" />
                <controls:TreeViewItem Header="Outbox"/>
                <controls:TreeViewItem Header="Sent Items"/>
                <controls:TreeViewItem Header="Search Folder" IsExpanded="True">
                  <controls:TreeViewItem Header="From Follow up"/>
                  <controls:TreeViewItem Header="Large Mail"/>
                  <controls:TreeViewItem Header="Unread Mail" />
                </controls:TreeViewItem>
              </controls:TreeViewItem>
            </controls:TreeView>
    
            <TextBox Name="txtTreeDoubleClickResult" Grid.Column="1" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible"></TextBox>
    
          </Grid>
    
        </Border>
    
        <Border BorderBrush="#FF000000" CornerRadius="5" BorderThickness="2" Background="#FFFFFFFF" Grid.Row="1" Margin="5">
          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition></ColumnDefinition>
              <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <ListBox MouseLeftButtonUp="ListBox_MouseLeftButtonUp">
              <ListBoxItem Content="Deleted"></ListBoxItem>
              <ListBoxItem Content="Unread"></ListBoxItem>
              <ListBoxItem Content="Mail"></ListBoxItem>
              <ListBoxItem Content="Junk"></ListBoxItem>
              <ListBoxItem Content="Folders"></ListBoxItem>
              <ListBoxItem Content="Drafts"></ListBoxItem>
            </ListBox>
    
            <TextBox Name="txtListDoubleClickResult" Grid.Column="1" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible"></TextBox>
          </Grid>
        </Border>
    
      </Grid>
    </UserControl>
    
    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 DoubleClickTest
    {
      public partial class MainPage : UserControl
      {
        private DoubleClickHelper treeDoubleClickHelper = new DoubleClickHelper();
        private DoubleClickHelper listDoubleClickHelper = new DoubleClickHelper();
        public MainPage()
        {
          InitializeComponent();
          treeDoubleClickHelper.OnDoubleClick += new EventHandler<DoubleClickEventArgs>(treeDoubleClickHelper_OnDoubleClick);
          listDoubleClickHelper.OnDoubleClick += new EventHandler<DoubleClickEventArgs>(listDoubleClickHelper_OnDoubleClick);
        }
    
        void listDoubleClickHelper_OnDoubleClick(object sender, DoubleClickEventArgs e)
        {
          txtListDoubleClickResult.Text +=
            (e.CurrentFrameworkElement as System.Windows.Controls.ListBoxItem).Content.ToString() + "\n";
        }
    
        void treeDoubleClickHelper_OnDoubleClick(object sender, DoubleClickEventArgs e)
        {
          TreeViewItem item = e.CurrentFrameworkElement as TreeViewItem;
    
          if (!item.HasItems)
          {
            txtTreeDoubleClickResult.Text += item.Header.ToString() + "\n";
          }
        }
    
        private void TreeViewItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
          FrameworkElement fe = e.OriginalSource as FrameworkElement;
    
          treeDoubleClickHelper.SendMessage(fe.ParentOfType<TreeViewItem>());
        }
    
        private void ListBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
          FrameworkElement fe = e.OriginalSource as FrameworkElement;
          listDoubleClickHelper.SendMessage(fe.ParentOfType<ListBoxItem>());
        }
      }
    }
    
    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 DoubleClickTest
    {
      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;
        }
      }
    }
    
    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.Collections.Generic;
    
    namespace DoubleClickTest
    {
      public static class UIElementExtensions
      {
        public static IList<T> ChildrenOfType<T>(this UIElement element) where T : UIElement
        {
          List<T> list = new List<T>();
          if (element != null)
          {
            element.ChildrenOfType<T>(ref list);
          }
          return list;
        }
    
        private static IList<T> ChildrenOfType<T>(this UIElement element, ref List<T> list) where T : UIElement
        {
          int childrenCount = VisualTreeHelper.GetChildrenCount(element);
          for (int i = 0; i < childrenCount; i++)
          {
            DependencyObject child = VisualTreeHelper.GetChild(element, i);
            if (child is T)
            {
              list.Add((T)child);
            }
            if (child is UIElement)
            {
              ((UIElement)child).ChildrenOfType<T>(ref list);
            }
          }
          return list;
        }
    
        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;
        }
      }
    
     
    
    }
    

    努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!
    2010年5月24日 2:31
    版主

全部回复

  • 不支持,可以使用MouseLeftButtonDown事件来实现:
    void Control_MouseLeftButtonDown (object sender, MouseButtonEventArgs e)
    {
      if ((DateTime.Now - _lastTime).TotalMilliseconds < DurationMillisecondLimitation)
      {
        OnDoubleClick (EventArgs.Empty);
      }
      _lastTime = DateTime.Now;
    }
    
    其中,DurationMillisecondLimitation是预设的双击时间间隔。
    2010年5月20日 9:01
  • 不支持

    <UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" x:Class="DoubleClickTest.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.openxmlformats.org/markup-compatibility/2006" 
      mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
      <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Border BorderBrush="#FF000000" CornerRadius="5" BorderThickness="2" Background="#FFFFFFFF" Grid.Row="0" Margin="5">
          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition></ColumnDefinition>
              <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
    
            <controls:TreeView x:Name="TreeView1" HorizontalAlignment="Left"
              Margin="10" VerticalAlignment="Top"            
                    MouseLeftButtonUp="TreeViewItem_MouseLeftButtonUp">
              <controls:TreeViewItem Header="Personal Folders" IsExpanded="True">
                <controls:TreeViewItem Header="Deleted Items"/>
                <controls:TreeViewItem Header="Drafts"/>
                <controls:TreeViewItem Header="Inbox" IsExpanded="True">
                  <controls:TreeViewItem Header="Folders" />
                </controls:TreeViewItem>
                <controls:TreeViewItem Header="Junk E-mails" />
                <controls:TreeViewItem Header="Outbox"/>
                <controls:TreeViewItem Header="Sent Items"/>
                <controls:TreeViewItem Header="Search Folder" IsExpanded="True">
                  <controls:TreeViewItem Header="From Follow up"/>
                  <controls:TreeViewItem Header="Large Mail"/>
                  <controls:TreeViewItem Header="Unread Mail" />
                </controls:TreeViewItem>
              </controls:TreeViewItem>
            </controls:TreeView>
    
            <TextBox Name="txtTreeDoubleClickResult" Grid.Column="1" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible"></TextBox>
    
          </Grid>
    
        </Border>
    
        <Border BorderBrush="#FF000000" CornerRadius="5" BorderThickness="2" Background="#FFFFFFFF" Grid.Row="1" Margin="5">
          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition></ColumnDefinition>
              <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <ListBox MouseLeftButtonUp="ListBox_MouseLeftButtonUp">
              <ListBoxItem Content="Deleted"></ListBoxItem>
              <ListBoxItem Content="Unread"></ListBoxItem>
              <ListBoxItem Content="Mail"></ListBoxItem>
              <ListBoxItem Content="Junk"></ListBoxItem>
              <ListBoxItem Content="Folders"></ListBoxItem>
              <ListBoxItem Content="Drafts"></ListBoxItem>
            </ListBox>
    
            <TextBox Name="txtListDoubleClickResult" Grid.Column="1" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible"></TextBox>
          </Grid>
        </Border>
    
      </Grid>
    </UserControl>
    
    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 DoubleClickTest
    {
      public partial class MainPage : UserControl
      {
        private DoubleClickHelper treeDoubleClickHelper = new DoubleClickHelper();
        private DoubleClickHelper listDoubleClickHelper = new DoubleClickHelper();
        public MainPage()
        {
          InitializeComponent();
          treeDoubleClickHelper.OnDoubleClick += new EventHandler<DoubleClickEventArgs>(treeDoubleClickHelper_OnDoubleClick);
          listDoubleClickHelper.OnDoubleClick += new EventHandler<DoubleClickEventArgs>(listDoubleClickHelper_OnDoubleClick);
        }
    
        void listDoubleClickHelper_OnDoubleClick(object sender, DoubleClickEventArgs e)
        {
          txtListDoubleClickResult.Text +=
            (e.CurrentFrameworkElement as System.Windows.Controls.ListBoxItem).Content.ToString() + "\n";
        }
    
        void treeDoubleClickHelper_OnDoubleClick(object sender, DoubleClickEventArgs e)
        {
          TreeViewItem item = e.CurrentFrameworkElement as TreeViewItem;
    
          if (!item.HasItems)
          {
            txtTreeDoubleClickResult.Text += item.Header.ToString() + "\n";
          }
        }
    
        private void TreeViewItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
          FrameworkElement fe = e.OriginalSource as FrameworkElement;
    
          treeDoubleClickHelper.SendMessage(fe.ParentOfType<TreeViewItem>());
        }
    
        private void ListBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
          FrameworkElement fe = e.OriginalSource as FrameworkElement;
          listDoubleClickHelper.SendMessage(fe.ParentOfType<ListBoxItem>());
        }
      }
    }
    
    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 DoubleClickTest
    {
      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;
        }
      }
    }
    
    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.Collections.Generic;
    
    namespace DoubleClickTest
    {
      public static class UIElementExtensions
      {
        public static IList<T> ChildrenOfType<T>(this UIElement element) where T : UIElement
        {
          List<T> list = new List<T>();
          if (element != null)
          {
            element.ChildrenOfType<T>(ref list);
          }
          return list;
        }
    
        private static IList<T> ChildrenOfType<T>(this UIElement element, ref List<T> list) where T : UIElement
        {
          int childrenCount = VisualTreeHelper.GetChildrenCount(element);
          for (int i = 0; i < childrenCount; i++)
          {
            DependencyObject child = VisualTreeHelper.GetChild(element, i);
            if (child is T)
            {
              list.Add((T)child);
            }
            if (child is UIElement)
            {
              ((UIElement)child).ChildrenOfType<T>(ref list);
            }
          }
          return list;
        }
    
        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;
        }
      }
    
     
    
    }
    

    努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!
    2010年5月24日 2:31
    版主