Posts

Showing posts from July, 2011

WPF C# Detect CTRL + A (PreviewKeyDown)

XAML: < Window  x : Class ="LassoSelection.Controls.MainWindow"  xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x ="http://schemas.microsoft.com/winfx/2006/xaml"  Title ="WPF C# Detect CTRL + A"            Height ="350"            Width ="525"            PreviewKeyDown ="WindowKeyDown">      < Grid >      Grid > Window > Code Behind: using  System.Windows; using  System.Windows.Input; public   partial   class   MainWindow {      public  MainWindow()     {         InitializeComponent();     }      private   void  WindowKeyDown( object  sender,  KeyEventArgs  e)     {          if  (e.Key ==  Key .A &&  Keyboard .Modifiers ==  ModifierKeys .Control)         {              MessageBox .Show( "CTRL + A Pressed!" );         }     } }   References: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx

WPF Get ListBoxItem from ListBox.SelectedItem using ItemContainerGenerator.ContainerFromItem

var  listBoxItem = listBox.ItemContainerGenerator.ContainerFromItem(myListBox.SelectedItem)  as   ListBoxItem ; if  (listBoxItem !=  null )  {      listBoxItem.Background =  SystemColors .HighlightBrush; }

Detecting drop (Drag and Drop between a WPF and a Non-WPF application)

Image
When you want to enable drag & drop between a WPF and a Non-WPF application where Drag is originated from the WPF application and Drop happens on the Non-WPF application, detecting when the Drop happened could be a challenge. You can achieve this 2 ways: Approach 1: DragDrop.DoDragDrop is a blocking call, thus when this call returns you can be sure that the drop happened. The drop could have happened in the same application or the other application, you would always know on the application that started the drag. Here is the DragDrop.DoDragDrop method signature: Code Sample: .... .... DragDrop .DoDragDrop(_draggedElt, data,  DragDropEffects .All); // Drop happened { } Approach 2: Before calling DragDrop .DoDragDrop attach the following event handler to the drag source:   var  queryhandler =  new   QueryContinueDragEventHandler (DragSourceQueryContinueDrag); _dragSource.QueryContinueDrag += queryhandler; While Drag is happening, the following event han

[Attached Property] WPF TextBox clears Text when user presses Escape key

To add the behavior to the WPF TextBox, use the following XAML. < Window  x : Class ="WPF.MainWindow"            xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x ="http://schemas.microsoft.com/winfx/2006/xaml"            Title ="WPF TextBox MVVM DataBinding Example"  Height ="500"  Width ="500"          xmlns : Behaviors ="clr-namespace:Behaviors">      < Grid >          < TextBox  Behaviors : TextBoxBehavior.EscapeClearsText ="True" />      > > using  System.Windows; using  System.Windows.Controls; using  System.Windows.Input; namespace   Behaviors {      public   class   TextBoxBehavior     {         #region  Attached Property EscapeClearsText          public   static   readonly   DependencyProperty  EscapeClearsTextProperty            =  DependencyProperty .RegisterAttached( "EscapeClearsText" ,  typeof ( bool ),  typeof ( TextBo

WPF TextBox MVVM DataBinding & use of UpdateSourceTrigger

If you are using a TextBox in your WPF application with MVVM pattern & want to keep the TextBox Text and the property in the ViewModel synchronized, you need to use UpdateSourceTrigger=PropertyChanged in the binding. Here is an example: View XAML: < Window  x : Class ="WPF.MainWindow"            xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x ="http://schemas.microsoft.com/winfx/2006/xaml"            Title ="WPF TextBox MVVM DataBinding Example"  Height ="500"  Width ="500">      < Grid >          < TextBox  Text ="{ Binding  Path =Name,  UpdateSourceTrigger =PropertyChanged,  Mode =TwoWay}"  Height ="25" />      Grid > Window > View Code Behind: using  WPF.ViewModel; namespace  WPF {      public   partial   class   MainWindow     {          public  MainWindow()         {             InitializeComponent();             DataContext =  new