views:

742

answers:

2

Does anybody know if it is possible to do databinding on an IValueConverter based class?

I have the following converter:

[ValueConversion(typeof(int), typeof(Article))]
public class LookupArticleConverter : FrameworkElement, IValueConverter {
    public static readonly DependencyProperty ArticlesProperty = DependencyProperty.Register("Articles", typeof(IEnumerable<Article>), typeof(LookupArticleConverter)); 

    public IEnumerable<Article> Articles
    {
        get { return (IEnumerable<Article>)GetValue(ArticlesProperty); }
        set { SetValue(ArticlesProperty, value); }
    }

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

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

It's purpose is to lookup an article in a list by its Id, and return that article.

However, I'd like to fill the Articles property by databinding a collection to it, like this:

<local:LookupArticleConverter Articles="{Binding Articles}" x:Key="LookupArticle"/>

But this doesn't seem to work. The setter method is never called. The source property contains an actual nonempty collection, so that's not the problem.

There's no error messages regarding binding in the output log, neither.

Any clues?

+2  A: 

WPF doesn't really support this intrinsically. However, there are some techniques that will allow you to do this, including Josh Smith's virtual branch approach.

HTH, Kent

Kent Boogaart
Thanks, that explains a lot.
Inferis
A: 

I'm pretty new to WPF and although I had a look at the virtual branch approach, I couldn't quite figure out how to apply it to the exact same issue that Inferis originally had asked - could you possibly post the solution that solved your issue?

Thanks a mill.