views:

259

answers:

3

What should be the best practice to declare the converters:

  1. Declare ALL my converters in the app.xaml (i.e. in <Application.Resources/>) so it's available in the entire application.
  2. Declare on each Page/Window/ResourceDictionary/DataTemplate etc. at its Resources section, the converters I need on this page.
  3. You tell me...

That's for sure (at least for my opinion) that regarding readability way no. 1 is the best, my question is about performance.

A: 

If you only need a converter for the one window, I would put it for the one window (or even for just the container control that holds the control that uses it).

I would argue that this is more maintainable - you can look at the converter declaration and be able to tell what uses it. You know that if you change the controls on that particular page to no longer use the converter, you can take it out of the page's resources without affecting anything else. Conversely, if a converter is an application resource, it's not so simple to ascertain what's using it, if anything.

If the same converter is being used by more than one page, I would still put it under each page resource. Really, it's only one extra line in the XAML.

Anyway, that's my opinion, as of today. I'm expecting another post arguing exactly the opposite. :-)

Andrew Shepherd
No such luck. My answer was half opposite, half the same :-P
DanM
+2  A: 

I have a ResourceDictionary that declares several commonly needed converters, such as a bool-to-visibility converter. I reference this dictionary directly in App.xaml.

I declare other converters that are more specific to a given situation at the Page/Window-level (or in a ResourceDictionary referenced by a Page/Window).

I can't answer the performance question definitively, but I would be very surprised if it made a practical difference in load time or memory usage. Declaring a converter is basically an object instantiation, so it should be very efficient and use very little memory, but I haven't done any profiling to compare app-level vs. window-level performance.

DanM
Well that was my question...If you will ever know the performance answer, please don't forget to come back.
Shimmy
+3  A: 

Well, I just don't declare them in xaml at all. Instead, I additionally derive a converter of mine from MarkupExtension. Like this:

public class MyValueConverter : MarkupExtension, IValueConverter
{
    private static MyValueConverter _converter = null;
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
     if (_converter == null) _converter = new MyValueConverter();    
     return _converter;
    }

    public object Convert
     (object value, Type targetType, object parameter, CultureInfo culture) { }
    public object ConvertBack
     (object value, Type targetType, object parameter, CultureInfo culture) { }
}

This allows me to use my converter anywhere, like this:

Source="{Binding myValue, Converter={converters:MyValueConverter}}"

where converters is the namespace in which I have declared my converter.

Learned this trick from an old stackoverflow thread only.

Yogesh
I just learned something, sure will be useful!
Shimmy
And yes, this is better in terms of performance as it does not instantiate a new object every time the converter is used. It only creates one instance before the return of the first call to the MarkupExtension and returns the same instance every time.
Yogesh
Thanks! This makes life easier.
Si