Hi

 

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>

In the Window Code Behind we create a list filled with some DateTime objects:

   1: public partial class Window1 : Window
   2:     {
   3:         public Window1()
   4:         {
   5:             InitializeComponent();
   6:             List<DateTime> d = new List<DateTime>();
   7:             d.Add(new DateTime(2005, 10, 2, 15, 22, 28));
   8:             d.Add(new DateTime(2002, 11, 5, 4, 55, 14));
   9:             d.Add(new DateTime(2006, 12, 3, 1, 55, 23));
  10:             d.Add(new DateTime(2004, 2, 1, 5, 26, 1));
  11:             d.Add(new DateTime(2001, 4, 6, 12, 35, 33));
  12:             d.Add(new DateTime(2004, 7, 3, 15, 28, 5));
  13:             this.DataContext = d;
  14:         }
  15:     }
  16: }

As you can see The template holds a UserControl, here is the UserControl Code:

   1: <UserControl x:Class="ConvertorLocation.UserControl1"
   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:     <UserControl.Resources>
   6:         <my:Convertor x:Key="CV"></my:Convertor>
   7:     </UserControl.Resources>
   8:    <Grid>
   9:     <Border Margin="5" BorderBrush="Black" BorderThickness="2" >
  10:         <TextBlock Text="{Binding Converter={StaticResource CV}}"></TextBlock>
  11:     </Border>
  12:     </Grid>
  13: </UserControl>

The converter converts the DateTime to universal time:

   1: public object Convert(object value, Type targetType, object parameter,
   2:                       System.Globalization.CultureInfo culture)
   3: {
   4:    return ((DateTime)value).ToUniversalTime();
   5: }

I have also added a Trace Print in the Converter Constructor so we can see how many time it is called:

   1: public Convertor()
   2: {
   3:     Debug.WriteLine("Converter Created");
   4: }

We have 6 Items in the list and this is what we got in the Visual Studio Output window:

Converter Created
Converter Created
Converter Created
Converter Created
Converter Created
Converter Created

6 “Converter Created” Prints in the output, Now Imagine you had 1000 Items in that list…

 

The Window will probably be created only once, but you can’t move the Converter and locate it in the Window, the UserControl won’t “see” it there. Your only option is to put in in App.Xaml.

Once moved to the App.Xaml file we get only one “Converter Created” Line in the Output, which means that only one instance of the Converter was created.

 

Enjoy

 

Amit

Tags :

3 Responses to “Why Should You Always Locate Your Convertors In App.Xaml”


  1. Martin

    Said on March 10, 2009 :

    Actually that is bad design. Put the Converters where they belong.

    To avoid the creation of multiple Converters simply use the singleton pattern, and then use {x:Static blaBla}.

  2. Amit

    Said on March 10, 2009 :

    That is another option which I have discussed in another post :http://www.dev102.com/2008/07/17/wpf-binding-converter-best-practices/

    I would say its bad design, it is just another option.

1 Trackback(s)

  1. Sep 6, 2009: WPF: 90+ Miejsc które warto zna? « Dawid Po?li?ski

Post a Comment