Posts

Showing posts from 2013

C# How to Serialize/Deserialize BitVector32

I have a class with a property of type BitVector32, when I Serialize it, I noticed the property was not getting serialized because the BitVector32 is not [Serializable]. After thinking about how can I serialize this property and looking into the implementation of BitVector32 it became clear that I need to approach this from a different angle. BitVector32 class has a property called Data where the information is stored, this property is of type Int and Int is obviously [Serializable]. Here is my approach: [ Serializable ]      public   class   TreeItem     {          // Serializable requires an empty constructor           public  TreeItem()         {         }          public   int  Id {  get ;  set ; }          public   int  ObjectType {  get ;  set ; }         [ XmlIgnore ]          public   BitVector32   Properties  {  get ;  set ; }         [ XmlElement ( "Properties" )]          public   Int32  Data         {              get  {  return  Propertie

WPF Telerik RadTreeView Select Node on right click

In an application that we are working on, we want to select the Telerik RadTreeViewItem node the user is right clicking on and display context menu for the selected node. This is not supported out of the box by Telerik, so I wrote an attached property to accomplish this. Please take a look at the code snippet below. Full Visual Studio 2010 solution is also uploaded on my google drive here . < Window  x : Class = "RadTreeViewExtensions.MainWindow"            xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"          xmlns : Telerik = "http://schemas.telerik.com/2008/xaml/presentation"          xmlns : RadTreeViewExtensions = "clr-namespace:RadTreeViewExtensions"          Title = "Telerik RadTreeView Behavior: Select Node on right click "            Height = "350"  Width = "525" >      < Grid >

C# How to unit test Dispatcher

Dispatcher provides services for managing the queue of work items for a thread. Sometimes to execute a task on a background thread.  Any task executed by calling Dispatcher.BeginInvoke(...) in unit test does not get executed until you do some wire-up. This is how you would unit test code with Dispatcher. using  System; using  System.Windows.Threading; using  Microsoft.VisualStudio.TestTools.UnitTesting; namespace  UnitTesting.Tests {     [ TestClass ]      public   class   DispatcherUtilTest     {          private   ClassThatUsesDispatcher  _subject;         [ TestInitialize ]          public   void  TestInitialize()         {             _subject =  new   ClassThatUsesDispatcher ();         }         [ TestMethod ]          public   void  UseDispatcher()         {              // Arrange              var  backgroundWorkDone =  false ;             _subject.BackgroungWorkAction = () => { backgroundWorkDone =  true ; };              // Act             _subject.DoWork(

WPF How to Dispose ViewModel when the associated UserControl (Not Window) closes?

Note: If your WPF UserControl is hosted in MFC Window, you may want to clean-up the ViewModel that is DataContext of your UserControl. Since Unloaded event is not called reliably for the UserControl when the hosting MFC Window closes, there is a need for another solution. XAML: < UserControl  x : Class = "Views.MyUserControl"               xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"               xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"               xmlns : mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"                 xmlns : d = "http://schemas.microsoft.com/expression/blend/2008"                 mc : Ignorable = "d"                 d : DesignHeight = "300"  d : DesignWidth = "300" >      < Grid >                   </ Grid > </ UserControl > Code Behind: using  System; using  ViewModels; name

C# How to create generic List with dynamic type

Given the Type typeof( Item ) (defined below), how would you create a list of Items using Reflection in C#? var  type =  typeof ( Item ); IList  listOfItems = ( IList ) Activator .CreateInstance( typeof ( List <>).MakeGenericType(type));   public   class   Item {      public   string  Name {  get ;  set ; } }     

WPF InputBindings KeyBinding Command Example .Net 4.0

Image
WPF starting from .Net Framework 4.0 allows developers to bind commands directly to the KeyBindings as shown in the example below: A complete solution is posted here on my Google Drive:  https://docs.google.com/file/d/0Bw72YL7u1BpmaFdsSng1N3Rqbnc/edit?usp=sharing < Window  x : Class = "WPF_InputBindings_Keybinding.MainWindow"          xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"          Title = "WPF InputBindings KeyBindings Example"  Height = "400"  Width = "400"  Focusable = "True" >      < Window.InputBindings >          < KeyBinding  Key = "C"  Modifiers = "Control"  Command ="{ Binding  CopyCommand } "  />          < KeyBinding  Key = "V"  Modifiers = "Control"  Command ="{ Binding  PasteCommand } "  />      </ Win

WPF: How to Style Horizontal GridSplitter

Image
< Window  x : Class = "HorizontalGridSplitterExample"          xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"          Title = "HorizontalGridSplitter Example"  Height = "300"  Width = "300" >      < Window.Resources >                   < Style  x : Key = "GridSplitterHorizontalGripStyle"  TargetType ="{ x : Type  GridSplitter } " >              < Setter  Property = "HorizontalAlignment"  Value = "Stretch"  />              < Setter  Property = "FocusVisualStyle"  Value ="{ x : Null } "  />              < Setter  Property = "Cursor"  Value = "SizeNS"  />              < Setter  Property = "FocusVisualStyle"  Value ="{ x : Null } "  />              < Setter  Property = "Backgr

WPF: How to Style Vertical GridSplitter

Image
< Window  x : Class = "VerticalGridSplitterExample"          xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"  Title = "VerticalGridSplitter Example"          Height = "300"  Width = "300" >      < Window.Resources >                   < Style  x : Key = "GridSplitterVerticalGripStyle"  TargetType ="{ x : Type  GridSplitter } " >              < Setter  Property = "VerticalAlignment"  Value = "Stretch"  />              < Setter  Property = "FocusVisualStyle"  Value ="{ x : Null } "  />              < Setter  Property = "Cursor"  Value = "SizeWE"  />              < Setter  Property = "FocusVisualStyle"  Value ="{ x : Null } "  />              < Setter  Property = "Background"