Steveluo’s Blog

Just another WordPress.com weblog

attached property

Posted by steveluo on February 24, 2009

 

treeview double click binding:
1: http://blogs.msdn.com/johngossman/
2 http://groups.google.com/group/wpf-disciples/browse_thread/thread/63cbe8b2b0575e4e?pli=1
3: http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/
  a smart way to use double click …

private static void OnMouseDoubleClickCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
           UIElement element = target as UIElement; if (element != null)
          {
                       
if ((e.OldValue == null) && (e.NewValue != null))
                       {  
                                   element.AddHandler(
UIElement.MouseDownEvent, new MouseButtonEventHandler(UIElementMouseLeftButtonDown), true);
                                   
//element.MouseLeftButtonDown += UIElementMouseLeftButtonDown;
                       
}
                      
else if ((e.OldValue != null) && (e.NewValue == null))
                       {
                                    element.RemoveHandler(
UIElement.MouseDownEvent, new MouseButtonEventHandler(UIElementMouseLeftButtonDown));
                                   /
/element.MouseLeftButtonDown -= UIElementMouseLeftButtonDown;
                      
}
           }

  
private static void UIElementMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
             // gets the sender is UIElement and check ClickCount because we want double click only

 

}
and

              UIElement target = sender as UIElement;
             
if (target != null && e.ClickCount > 1)
              {
        
                 ICommand iCommand = (ICommand)target.GetValue(MouseDoubleClickCommandProperty);
                       
if (iCommand != null)
                       {  
                                 
RoutedCommand routedCommand = iCommand as RoutedCommand;
                               
// check if the command has a parameter using the MouseEventParameterProperty         
                               
object parameter = target.GetValue(MouseEventParameterProperty);

                               // execute the command
                             
if (parameter == null)
                             {       parameter = target;
                             }

                             if (routedCommand != null)
                             {
                                     routedCommand.Execute(parameter, target);
                                }
                             else
                            {
                                   iCommand.Execute(parameter);                              }

 

                                  e.Handled =  true;

 

                      }

}

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: