Posts

Showing posts from January, 2013

WPF Drag & Drop: How to detect when drag is started

Image
This article will explain how to detect when drag is started in WPF Applications. Features: 1) Reliably detect drag & drop 2) Don't confuse double clicks as drag start 3) Don't confuse scrolling as drag event  Notes: - WPF is very powerful, but one thing i find missing is an event telling developers when drag is started - The solution here involves handling Left Button Down, Mouse Move & Left Button Up event combinations to detect when drag & drop is started and when it ended Important: Even though the code posted here uses code-behind, I highly recommend using MVVM way of handling drag & drop using attached properties on controls to detect drag & drop, show visual feedback and transfer data. (I will post a WPF MVVM Drag & Drop Framework soon) Complete Visual Studio 2010 Solution https://docs.google.com/file/d/0Bw72YL7u1BpmQkl3Q3BleDgzR1E/edit?usp=sharing XAML: < Window  x : Class = "DragAndDropDragStart.MainWind

C# how to get type of generic class using reflection

In the following example, given an instance of SystemFolder, how would you find the generic type Folder given that SystemFolder  :  IDropTarget < Folder >? using  System; using  System.Collections.ObjectModel; using  System.Diagnostics; using  System.Linq; namespace  CSharpGenerics { public   class   GenericUtilities     {          // Example Type type = GenericUtilities.GetGenericType(new SystemFolder()); will return Folder          public   static   Type  GetGenericType( object  @object)         {              var  type = @object.GetType();              var  iDropTargetObject = type.GetInterfaces(). FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() ==  typeof ( IDropTarget <>));              Debug .Assert(iDropTargetObject !=  null );              return  iDropTargetObject.GetGenericArguments()[0];         }     }      public   class   SystemFolder  :  IDropTarget < Folder >     {          public  SystemFolder()         {  

WPF: Scroll content of control when drag & drop is in progress

It's nice to be able to scroll through content of a control when items are being dragged over a WPF control.  I have created an attached property called  "IsScrollOnDragOverEnabled", which you can set on a control with ScrollViewer as shown below to scroll the content of the control when items are being dragged over the control. Features: 1) Horizontal Scroll 2) Vertical Scroll 3) Smooth Scroll (Note: This feature scrolls content of the control in a smooth scroll fashion, to let the users see and interact with content being scrolled. Here is how to use it: < Window  x : Class = "DragAndDropFramework.MainWindow"  xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"          xmlns : DragAndDropBehaviors = "clr-namespace:DragAndDropBehaviors;assembly=DragAndDrop"  Title = "Drag and Drop Demo"          Height = "5

WPF: VisualTreeHelper Find Parent of Specific Type

While working with WPF Visual Tree, one of the common tasks is to find the Parent(Ancestor) or Child of any specific element (DependencyObject). The following piece of code traverses the visual tree recursively and finds the type of parent you are looking for related to the element you pass in. For example: If the Visual Tree hierarchy is like this: Grid StackPanel ListBox You can call the function like this to find the Grid: FindAncestor (( DependencyObject )ListBox) ; public   static  T FindAncestor ( DependencyObject  dependencyObject)              where  T :  DependencyObject         {              var  parent =  VisualTreeHelper .GetParent(dependencyObject);              if  (parent ==  null )  return   null ;              var  parentT = parent  as  T;              return  parentT ?? FindAncestor (parent);         }

C#: Executing action multiple times with exception handling

There are times when you want to execute a function multiple times while watching out for any exceptions. public void ExecuteAction(Action action, int numberOfTries = 2) { if (numberOfTries <= 0) return; try { action(); } catch { ExecuteAction(action, --numberOfTries); } } You can use the function like this ExecuteAction(() => Clipboard.SetText("Your Text"), 2); One of those scenarios is when you are trying to store some text on clipboard like this: try { Clipboard.SetText("Your Text"); } catch (System.Runtime.InteropServices.COMException) { try { Clipboard.SetText("Your Text"); } catch (System.Runtime.InteropServices.COMException) { } }

WPF: .exe does not contain a static Main method suitable for an entry point

Image
This morning while refactoring my WPF application, I ran into the following error: "  .exe does not contain a static Main method suitable for an entry point " If you are getting the same error the solution is simple, you need to make sure that the " Build Action " from App.xaml file for your application is set to " ApplicationDefinition "