XAML markup binding to dictionary with key of type Type

折月煮酒 提交于 2019-11-30 07:24:55

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}}}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!