I have data templates that looks like:
<DataTemplate>
<TextBlock DataContext="{Binding Fields[ABC]}" Text="{Binding}"/>
</DataTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding Fields[)]}" Text="{Binding}"/>
</DataTemplate>
For a class that looks like
class Source {
public Dictionary<string, string> Fields { get; private set; }
}
When applying the second template, with the ‘)’ key in the DataContext binding, I get a XamlParseException. Is there any way to allow Dictionary Binding to work with other strings such as ‘)’ ? Some sort of escape character sequence?
You could construct a valid path by using path parameters, this makes sure the parenthesis is passed as a string and not part of the path description. The easiest way to do that would probably be via a custom markup extension as shown in this answer of mine.
The binding then could be written as:
(Quotes around the parenthesis are optional but make it more readable i think)