Skip to main content

Posts

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]     publicclassTreeItem     {         // Serializable requires an empty constructor public TreeItem()         {         }         publicint Id { get; set; }         publicint ObjectType { get; set; }         [XmlIgnore]         publicBitVector32Properties { get; set; }         [XmlElement("Properties")]         publicInt32 Data         {             get { return Properties.Data; }             set { Properties = newBitVector32(value); }         }     }

Here is…
Recent posts

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><Grid.Resources><HierarchicalDataTemplate x:Key="ChildrenTemplate" ItemsSource="{Binding Pat…

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]     publicclassDispatcherUtilTest     {         privateClassThatUsesDispatcher _subject;         [TestInitialize]         publicvoid TestInitialize()         {             _subject = newClassThatUsesDispatcher();         }         [TestMethod]         publicvoid UseDispatcher()         {             // Arrangevar backgroundWorkDone = false;             _subject.BackgroungWorkAction = () => { backgroundWorkDone = true; };             // Act             _subject.DoWork();             // Assert// Without calling DispatcherUtil.DoEvents() …

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; namespace Views {     publicpartialclassMyUserControl     {         public MyUserControl()         {             DataContext = newMyUserControlViewModel();             InitializeComponent(); …

WPF InputBindings KeyBinding Command Example .Net 4.0

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}" /></Window.InputBindings></Window>

WPF: How to Style Horizontal GridSplitter

<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="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /><Setter

WPF: How to Style Vertical GridSplitter

<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" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /><Setter Property="Template">