3

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?

Sheldon Warkentin
  • 1,706
  • 2
  • 14
  • 31
  • I would try a straight pass thru converter and see if that does not somehow bypass this parse exception. Or convert with a Linq strOut = dl.FirstOrDefault(kvp => kvp.Key == "(").Value; In know ugly but if it works better than nothing. – paparazzo Mar 01 '12 at 17:38
  • @jberger: That won't work as it throws this error: `Error 1 Names and Values in a MarkupExtension cannot contain quotes. The MarkupExtension arguments ' Fields[')']}' are not valid.` – H.B. Mar 01 '12 at 17:44
  • i had seen something similar used in a recent answer, so worth a shot – Jake Berger Mar 01 '12 at 18:12

1 Answers1

2

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:

{Binding Path={me:PathConstructor Fields[(0)],')'}}

(Quotes around the parenthesis are optional but make it more readable i think)

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Thanks, this helped point me in the right direction. I guess the real issue in the end was that some of the dictionary elements had leading/trailing spaces in them. This PathConstructor format helps resolve this. – Sheldon Warkentin Mar 01 '12 at 22:24