When working with WPF I always found myself thinking how to handle Data formatting when a WPF control was bound to it. Let’s look at the following example of a window with a TextBlock that displays a DateTime:

   1: <Window x:Class="BindingFormat.Window1"

   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   4:     Title="Window1" Height="300" Width="300">

   5:     <Grid>

   6:         <TextBlock Text="{Binding}"></TextBlock>

   7:     </Grid>

   8: </Window>

Continue Reading...



A while ago Shahar wrote an article about whether WPF Data Binding is Thread safe. Shahar’s findings were that Even if you change a property from a different thread the PropertyChanged event will be called on the UI Thread making Binding Thread Safe.

I have created a Window with 2 TextBlocks, one of them is binded to a Dependency Property and the other is binded to a regular property:

The Window:

   1: <StackPanel>
   2:     <TextBlock Text="{Binding DpTxt}" Width="100" Margin="5"></TextBlock>
   3:     <TextBlock Text="{Binding Txt}" Width="100" Margin="5"></TextBlock>
   4:     <Button Content="Change Text" Margin="5" Click="Button_Click"></Button>
   5: </StackPanel>

Continue Reading...



Where do you locate your convertors? do you put them in the Window.Resources section? or in the UserControl they are being used for? Neither of these options is good. You should put it in the App.Xaml file and here is why:

Lets look at the following example:

Here is our Window:

   1: <Window x:Class="ConvertorLocation.Window1"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:         xmlns:my="clr-namespace:ConvertorLocation"
   5:     Title="Window1" Height="300" Width="300">
   6:     <Grid>
   7:         <ItemsControl ItemsSource="{Binding}">
   8:             <ItemsControl.ItemTemplate>
   9:                 <DataTemplate>
  10:                     <my:UserControl1></my:UserControl1>
  11:                 </DataTemplate>
  12:             </ItemsControl.ItemTemplate>
  13:         </ItemsControl>
  14:     </Grid>    
  15: </Window>

Continue Reading...

Lately I have been playing around with serializing and deserializing of objects, and I stumbled upon a very weird behavior. I was trying to serialize a certain object that was implementing the INotifyPropertyChanged Interface. I marked the object as [Serializable] and used the following method to serialize it:

   1: public void SerializeNow()
   2: {
   3:    Stream s = File.Create("Data.dat");
   4:    BinaryFormatter b = new BinaryFormatter();
   5:    b.Serialize(s, data);
   6:    s.Close();
   7: }

Continue Reading...


In my previous post about WPF Binding Converters one of our readers (Thanks The Reddest) pointed our that a new converter instance is not created every time a call is made to the converter. I promised to test it again so here goes :)
I used the old converter code but added a static integer inside the converter to count the number of calls made to it. and then printed it out to the trace in the constructor.

Continue Reading...

Have you ever tried to create a DataTemplate for a Generic Class? During last week I had to battle this issue and it is allot more complicated then it sounds. As far as I can tell Creating DataTemplates for Generic classes is impossible. There is some kind of workaround but it is not all that good. OK lets get down to business. This is the class we are trying to template

   1: public class GenericClass<T>
   2: {
   3:     private T m_Val;
   5:     public T Val
   6:     {
   7:         get { return m_Val; }
   8:         set { m_Val = value; }
   9:     }
  10: }

Continue Reading...

Hi all

As you all remember in my article about Custom WPF Context Menu I mentioned that my WPF Binding Converter was a singleton, and I promised to tell you why, so here comes the 3 ways I know of using WPF Binding Converters. We will start from the worst (in my opinion) and move on the the best.

To start off here is the Binding Converter:

Continue Reading...

How would you like your WPF application to have a Context Menu like this:

Context Menu Example

Well, It is not so hard. Here is how you do it:

Continue Reading...

Every one of us, software developers, experienced situations where the .Net Framework could not locate an assembly and ended up facing the TypeLoadException. These failures usually happen due to an assembly deployed to the wrong location or a mismatch in version numbers or cultures. A quick way to check what went wrong is to open the module window (Visual Studio) during debugging but that may be sometimes impossible or inconvenient because:

  • We may not have Visual Studio installed. 
  • We installed the product in the customer site and we don’t have the code available.
  • It is some third party assemblies which causes the problems.

Luckily, there is an assembly binding log viewer which displays information that helps us diagnose why the .NET Framework can not locate an assembly at run time. This tool is called

Continue Reading...

We have all used WPF DataTemplates, but I bet most of you never gave thought to the meaning of the x:Key attribute. We all know the meaning of the DataType attribute, which determines to what type of data the template will apply to. But what about the x:Key why is it there? Lets tale a look at the following code:

        <DataTemplate DataType="{x:Type data:s}">
            <Button Width="50" Height="50">
                <TextBlock Text="{Binding text}"></TextBlock>
        <ItemsControl ItemsSource="{Binding}">

Continue Reading...