Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML markup binding to dictionary with key of type Type

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)

like image 221
Markus Hütter Avatar asked Feb 21 '12 02:02

Markus Hütter


2 Answers

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}}}
like image 133
H.B. Avatar answered Oct 24 '22 18:10

H.B.


update: I leave this up for reference for extending Bindings

<Grid Width="{my:ParameterBinding {Binding [(0)],Source={x:Static my:SettingsService.Current}, Mode=TwoWay},{x:Type my:LeftPanelWidthSetting}}"/>

and that's the code behind

[ContentProperty( "Parameters" )]
public class ParameterBinding : MarkupExtension
{
    public Binding Binding { get; set; }
    public IList Parameters { get; set; }

    public ParameterBinding()
    {
        Parameters = new List<object>(); 
    }

    public ParameterBinding( Binding b, object p0 )
    {
        Binding = b;
        Parameters = new []{p0};
    }

    public override object ProvideValue( IServiceProvider serviceProvider )
    {
        Binding.Path = new PropertyPath( Binding.Path.Path, Parameters.Cast<object>().ToArray() );
        return Binding.ProvideValue(serviceProvider);
    }
}

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

like image 23
Markus Hütter Avatar answered Oct 24 '22 19:10

Markus Hütter