If the indexer has a specific type the conversion should be done automatically so this should work:
{Binding theDictionary[ns:OnePrettyType]}
If you need an explicit interpretation you can try a "cast" like this:
{Binding theDictionary[(sys:Type)ns:OnePrettyType]}
(Where sys
is mapped to the System
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 of PropertyPath
in a certain way. Also you will get a binding error:
System.Windows.Data Error: 40 : BindingExpression path error: '[]' property not found on 'object' ''Dictionary`2'
You would need to make it construct the PropertyPath
via the type converter by avoiding the Binding
constructor:
{Binding Path=theDictionary[(sys:Type)ns:OnePrettyType]}
Now this will most likely just throw an exception:
{"Path indexer parameter has value that cannot be resolved to specified type: 'sys:Type'"}
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.
[ContentProperty("Parameters")]
public class PathConstructor : MarkupExtension
{
public string Path { get; set; }
public IList Parameters { get; set; }
public PathConstructor()
{
Parameters = new List<object>();
}
public PathConstructor(string path, object p0)
{
Path = path;
Parameters = new[] { p0 };
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new PropertyPath(Path, Parameters.Cast<object>().ToArray());
}
}
Which then can be used like this:
<Binding>
<Binding.Path>
<me:PathConstructor Path="theDictionary[(0)]">
<x:Type TypeName="ns:OnePrettyType" />
</me:PathConstructor>
</Binding.Path>
</Binding>
or like this
{Binding Path={me:PathConstructor theDictionary[(0)], {x:Type ns:OnePrettyType}}}