tags:

views:

886

answers:

1

I have an IValueConverter whose job it is to convert a BlockId to a ConditionLabel. The problem is that my Model object is what has the smarts to do the actual conversion. My code looks like this so far...

public class BlockIdToConditionLabelConverter : IValueConverter
{
    private Model _model;

    public BlockIdToConditionLabelConverter(Model model)
    {
        _model = model;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int blockId = (int)value;
        return _model.BlockIdToConditionLabel(blockId);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

At the moment, I create a static resource in a ResourceDictionary like this (and later refer to it in a DataTemplate):

<local:BlockIdToConditionLabelConverter
    x:Key="_blockIdToConditionLabelConverter" />

The problem is, I need a way to pass my Model object into this converter. How would I do that?

Thanks.

+3  A: 

This is a classic problem with value converters.

If you are using MVVM pattern, you can solve that problem by implementing the conversion inside the ViewModel of your model.

If not, you can take a look at this post on MSDN forums. (answer from Sam Bent - MSFT)

The goal is to use multibinding to pass the model to your converter, in this case, pass the DataContext.

decasteljau
Well now you have me reading up on MVVM :) It might make a lot of sense for me to start using it, because I was running into exactly the problem the pattern seems to want to solve: Where do I put my converter logic? I haven't read enough yet to quite understand what you mean by "implementing the conversion inside the ViewModel of your model." Does this mean I don't use IValueConverters at all? Or just that the ViewModel (rather than the XAML markup) has the job of instantiating them? Thanks!
DanM
I am not sure how your Model class is implemented.In MVVM, instead of having the model as your DataContext in WPF, you would have the ViewModel as your DataContext. I am guessing you were originally binding the BlockId property of your model. Your ViewModel would have a property ConditionLabel, that you would bind instead of BlockId. This property would do the conversion inside the getter.Your ViewModel would be registered to PropertyChanged of your model, so you can RaisePropertyChange("ConditionLabel") in your ViewModel.
decasteljau
I've read some more on MVVM, so this is starting to make sense. I could just store the converted data in my ViewModel and do away with IValueConverters completely. One catch, though, is that I was planning to use this IValueConverter with a GridView, so I'd need to store a whole table of converted data in my ViewModel (not just a single property). Would an IValueConverter still make sense for that? And what if I want to turn all GridView cells where the Block is complete light gray? Even if I stored an "IsComplete" table in the ViewModel, I'd still need to convert that to a color.
DanM