0

To start, understand that I'm dealing with a dynamic interface that is generated at run-time from XML. I have written XAML that switches on various elements and attributes to create the appropriate controls and labels, in order to display the data contained in the augmented XML file.

Sample XML fragment:

   <floop Format="combo" Choices="apple;banana;cherry" Value="cherry"/>

Format and value works already. I'm looking for a way to make Choices work. (Yes this is unconventional for XML, but it serves the purpose for now.)

Here's a proposed XAML fragment:

 <ComboBox Style="{StaticResource ComboButtonStyle}"  
         Width="200"
         ItemsSource="{Binding XPath=@Choices}" IsEditable="True"
         />

The thing is, @Choices is a single string, so I'm proposing to parse the string to produce the type of data ItemsSource wants.

How do I get there from here?

1 Answer 1

1

You want to use a converter on the Binding to take it from a string to a list. This answer does the opposite.

I haven't tested this code but it should get you where you want to go.

<ComboBox Style="{StaticResource ComboButtonStyle}" Width="200" ItemsSource="{Binding XPath=@Choices, Converter={StaticResource StringToListConverter}}" IsEditable="True" />

[ValueConversion(typeof(string), typeof(List<string>))]
public class StringToListConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (targetType != typeof(List<string>))
            throw new InvalidOperationException("The target must be a List<string>");

        return new List<string>(((string)value).Split(';'));
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

To fix, I added <W3V:StringToListConverter x:Key="stringToListConverter"/> to Resounces, and changed the Converter reference to Converter={StaticResource stringToListConverter}
Okay next problem: Convert is receiving an XmlDataCollection and a System.Collections.IEnumerable, not string and List<string>. I can switch the types on the latter, but what's up with this XmlDataCollection?
@AlanBaljeu Looks like it will use that as opposed to just a string. I wouldn't worry about it. Try changing string to XmlDataCollection and see if you can get it to work. You will have to tweak the code a bit. As for the converter, I knew you would have to make additional changes but how you did it is appropriate.
For future reference what I eventually did: Remove XPath=@choices, and then receive an XML element in the converter. From there I can write C# to access the attribute from the XmlElement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.