Take the 2-minute tour ×

Sorry for the cryptic title - let me explain a bit more:

I have a general-use c# class:

public class ContentIngestSource
    public int Id { get; set; }
    public string SourceType { get; set; }
    public Dictionary<string,string> SourceProperties { get; set; } //name/value

SourceProperties is simply a property bag that can contain different name/value pairs depending on the SourceType. Eg for SourceType 'GoogleSpreadsheet', SourceProperties may contain:

{ { "key", "afd53g.." }, { "visibility", "public" } }

What I'm trying to achieve is to keep all the code (server/ria/SL) general, except for the SL DataTemplates (ie I dont want to write a GoogleSpreadsheetIngestSource model). I have a ContentControl subclass that selects the right DataTemplate for me based on the SourceType. That bit works fine.

The problem I have is getting elements in the DataTemplate to two-way bind to the items in the SourceProperties list.

I can get one-way binding using a ValueConverter, but I have no idea how to make this work two-way???:

public class CollectionPickerConverter : IValueConverter
    //this method works
    public object Convert(object value, Type targetType, object parameter, ...)
        if (value == null) return null;

        if (!(value is Dictionary<string,string>))
            throw new ArgumentException("Source must be of type Dictionary<string,string>");

        var props = (Dictionary<string,string>)value;
        var key = (string)parameter;

        if (string.IsNullOrEmpty(key))
            throw new ArgumentException("Parameter must be non-blank");

        if (props.ContainsKey(key))
            return props[key];
            return "";

    public object ConvertBack(object value, Type targetType, object parameter, ...) 
        //not sure this bit can be done via a ValueConverter !?!?
        //- if I return a dictionary with just a single entry, Ill lose the rest of the
        //entries in the SourceProperties collection

A DataTemplate can then be bound using, eg:

<TextBlock Text="Visibility:" />
<TextBox Text="{Binding SourceProperties, 
                Converter={StaticResource collectionPickerConverter}, 
                ConverterParameter=visibility}" />

where 'visibility' is the key to be selected in the dictionary.

Ive searched high and low for solutions to this problem but havent found anything. Possibly theres a simple soln that Im missing?

I know I could implement strongly typed models, but there are potentially a lot of them and my customer wants the code kept as maintenance free as possible, ie when a new SourceType is added only a dataTemplate needs adding for it.

share|improve this question
Let me be clear: what you do is to convert a string dictionnary to a single string for display purpose. At this point you have ALREADY lose the rest of the entries in the sourceProperties collection. Thus, there is no way to convert back a single string into a dictionnary of string! What are exactly the information in these dictionnary? Why do you need them? –  Ouarzy Feb 26 '13 at 7:49
The idea is to have different dataTemplates for the different 'types' of ContentIngestSource objects (the type is distinguished by the SourceType string). Instead of strongly-typed, sourceType-specific properties, a property bag (SourceProperties) is used to hold properties relevant to the type. I need a way to two-way bind to the correct properties get the properties out and back in when edited. I have a feeling this simply cannot be accomplished using a ValueConverter. Strongly typed Models is prob the only way forward. –  crotchy Feb 26 '13 at 16:55
add comment


Your Answer



Browse other questions tagged or ask your own question.