This is a similar problem for WPF Binding: casting in the binding path , where I need to pass the object to the XAML binding operator. But I canβt figure out how to create a binding in my specific case.
The answer to this question refers to the PropertyPath XAML Syntax , and the corresponding section (I believe) is Single Property, Attached or Otherwise Type-Qualified .
In my case, in my main view model, I have a dictionary that displays strings for objects that implement the base interface:
Dictionary<string, IFlintStone> FlintStones { get; set;} public interface IFlintStone { Walk, Talk etc} public class FlintStone : IFlintStone { .. }
However, I also have these additional objects and interfaces that are subclasses of the base object:
public interface IFred : IFlintStone { Eat, Drink, Yell etc } public interface IWilma : IFlintStone { Wash, Clean, Cook etc } public class Fred : FlintStone, IFred {..} public class Wilma : FlintStone, IWilma {..}
And in the end I fill out my dictionary like:
FlintStones["Fred"] = new Fred(); FlintStones["Wilma"] = new Wilma();
Now in my XAML, I have a user control for rendering the Fred object, and another for rendering the Wilma object. I can set the data context of these user controls like this:
<FredControl DataContext="{Binding Path=FlintStones[Fred]}" /> <WilmaControl DataContext="{Binding Path=FlintStones[Wilma]}" />
However, I understand that this will result in the export of the IFlintStone components of these objects to the corresponding user controls. But I want to show IFred in <FredControl> and IWilma in <WilmaControl>
Is this possible and what is the binding syntax in this case?
Using the ideas from the links to which I referred above, I tried things like:
<FredControl DataContext="{Binding path=(myns:Fred.FlintStones[Fred])}" />
and
<FredControl DataContext="{Binding path=(myns:Fred).FlintStones[Fred]}" />
(Where myns is the myns namespace definition that points to the Fred object in the assembly.)
But the program starts or burns at startup, or complains that it cannot find Fred as a property of the current data context.