0
votes

I want bind to the Converter property rather than use {StaticResource ResourceKey} for its value.

Actually, I have a ListView with custom UserControl as ItemTemplate. Items use a ItemConverter : IValueConverter for binding. When I declare my converter in UserControl.Resources, an instance of ItemConverter gets created for every list item, which is absolutely unnecessary. I'd like to create a single converter instance and pass it to every item, so I can do the following inside my usercontrol:

<!-- not working -->
<TextBlock 
    Text="{Binding Converter={Binding something}}"
    Foreground="Black"
    FontSize="40"
    />

Is it possible to do that somehow in a Universal Store Application for Windows 8.1 and Windows Phone 8.1? Any ideas how to avoid doing this altogether?

I have found some outdated projects that are not compatible with universal apps:

Is there something like that for universal apps?

2

2 Answers

0
votes

I'd like to create a single converter instance

I wrote an article on my blog (Xaml: Call Binding Converter Without Defining StaticResource in Xaml Thanks to Markup Derived Base Class in C#) on a method to such an operation.

Let me paraphrase my own article.


Once a converter is defined using a base classes' singleton, one does not have to create a converter in the resources of a page(s).

public class BooleanToVisibilityReverseConverter : 
                         CoverterBase<BooleanToVisibilityReverseConverter>, 
                         System.Windows.Data.IValueConverter

Here is an example of access in Xaml (no static created in page's resources) just a simply namespace access to the converter:

<DataGrid Grid.Row="1"
          Visibility="{Binding IsEditing, 
                       Converter={ converters:BooleanToVisibilityReverseConverter } 
                      }">

Subsequently the access is direct in the namespace and only one is created for every page.

Here is the base class:

/// <summary>
/// This creates a Xaml markup which can allow converters (which inheirit form this class) to be called directly
/// without specify a static resource in the xaml markup.
/// </summary>
public  class CoverterBase<T> : MarkupExtension where T : class, new()
 {
    private static T _converter = null;

    public CoverterBase() { }

    /// <summary>Create and return the static implementation of the derived converter for usage in Xaml.</summary>
    /// <returns>The static derived converter</returns>
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return _converter ?? (_converter = (T) Activator.CreateInstance(typeof (T), null));
    }
}

For a better overview please see my blog article.

0
votes

The feature in question is not available at the moment for Universal Store Projects for Windows 8.1 and Windows Phone 8.1.