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); }
}
}
}