WPF cast in binding path

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.

+5
source share
1 answer

Here is a working version of my interpretation of your problem:

MainWindow.xaml

 <Window x:Class="WpfApplication22.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication22" Title="MainWindow" Height="350" Width="525"> <StackPanel> <TextBlock Text="{Binding Path=FlintStones[Fred].FlintStone}" /> <TextBlock Text="{Binding Path=FlintStones[Fred].(local:Fred.Yell)}" /> <local:FredControl DataContext="{Binding Path=FlintStones[Fred]}" /> <TextBlock /> <TextBlock Text="{Binding Path=FlintStones[Wilma].FlintStone}" /> <TextBlock Text="{Binding Path=FlintStones[Wilma].(local:Wilma.Clean)}" /> <local:WilmaControl DataContext="{Binding Path=FlintStones[Wilma]}" /> </StackPanel> </Window> 

MainWindow.xaml.cs

 using System.Windows; using System.Collections.Generic; namespace WpfApplication22 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public Dictionary<string, IFlintStone> FlintStones { get; set; } public MainWindow() { InitializeComponent(); FlintStones = new Dictionary<string, IFlintStone>(); FlintStones["Fred"] = new Fred(); FlintStones["Wilma"] = new Wilma(); this.DataContext = this; } } public interface IFlintStone { string FlintStone { get; set; } } public interface IFred : IFlintStone { string Yell { get; set; } } public interface IWilma : IFlintStone { string Clean { get; set; } } public class Fred : IFred { public string FlintStone { get; set; } public string Yell { get; set; } public Fred() { FlintStone = "Fred Flintstone"; Yell = "Yabba Dabba Doo"; } } public class Wilma : IWilma { public string FlintStone { get; set; } public string Clean { get; set; } public Wilma() { FlintStone = "Wilma Flintstone"; Clean = "Mammoth Dish Washer"; } } } 

FredControl.xaml

 <UserControl x:Class="WpfApplication22.FredControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <StackPanel Background="Beige"> <TextBlock Text="{Binding FlintStone}" /> <TextBlock Text="{Binding Yell}" /> </StackPanel> </UserControl> 

Wilmacontrol.xaml

 <UserControl x:Class="WpfApplication22.WilmaControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <StackPanel Background="AntiqueWhite"> <TextBlock Text="{Binding FlintStone}" /> <TextBlock Text="{Binding Clean}" /> </StackPanel> </UserControl> 

FredControl.xaml.cs and WilmaControl.xaml.cs are unmodified (only InitializeComponent (), in the constructors).

And screenshot:

enter image description here

+9
source

Source: https://habr.com/ru/post/1204204/


All Articles