I'm trying to bind to a Dictionary<Type,string>
through xaml.
The problem is, the indexer []
in the Binding
markup extension interprets it's content as string. Is there some kind of 'unescape-sequence' for that case?
<TextBox Text="{Binding theDictionary[{x:Type ns:OnePrettyType}]}" />
(Binding doesn't work because {x:Type ns:OnePrettyType}
is being sent as string)
If the indexer has a specific type the conversion should be done automatically so this should work:
If you need an explicit interpretation you can try a "cast" like this:
(Where
sys
is mapped to theSystem
namespace of course)That would be the theory but all of that won't work. First of all if you use the
Binding
constructor that takes a path the cast will be ignored as it uses a certain constructor ofPropertyPath
in a certain way. Also you will get a binding error:You would need to make it construct the
PropertyPath
via the type converter by avoiding theBinding
constructor:Now this will most likely just throw an exception:
So there unfortunately is no default type conversion going on. You could then construct a
PropertyPath
in XAML and make sure a type is passed in, but the class is not meant to be used in XAML and will throw an exception if you try, also very unfortunate.One workaround would be to create a markup extension which does the construction, e.g.
Which then can be used like this:
or like this
update: I leave this up for reference for extending Bindings
and that's the code behind
this would be extendable to support more parameters in inline syntax with additional constructors. I still left in the ability to add many parameters with the extended element syntax.
thx to H.B. for inspiring this