2014-10-07 120 views
5

这是一个类似于WPF Binding : Casting in binding path的问题,我需要在XAML绑定语句中强制转换对象。但我似乎无法理解如何在我的特定情况下制作装订。WPF Casting in Binding Path

该问题的答案参考PropertyPath XAML Syntax,相关部分是(我相信)Single Property, Attached or Otherwise Type-Qualified

对我来说,在我的主视图模型我有一个映射字符串实现基本接口的对象字典:

Dictionary<string, IFlintStone> FlintStones { get; set;} 

public interface IFlintStone { Walk, Talk etc} 
public class FlintStone : IFlintStone { .. } 

不过我也有这些额外的对象和接口继承的基础对象:

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

,最终我填充我的字典,如:

FlintStones["Fred"] = new Fred(); 
FlintStones["Wilma"] = new Wilma(); 

现在,在我的XAML中,我有一个用于呈现对象Fred的用户控件,另一个用于呈现对象Wilma。但是我的理解是,这只会暴露这些对象的IFlintStone组件各自的用户控件

<FredControl DataContext="{Binding Path=FlintStones[Fred]}" /> 
<WilmaControl DataContext="{Binding Path=FlintStones[Wilma]}" /> 

:我可以设置这些用户控件做类似的数据上下文。但是,我要公开IFred<FredControl>IWilma<WilmaControl>

这是可能的,并会结合语法,在这种情况下怎么办?

使用从我上面提到的链接的想法,我已经试过的东西,如:

<FredControl DataContext="{Binding path=(myns:Fred.FlintStones[Fred])}" /> 

<FredControl DataContext="{Binding path=(myns:Fred).FlintStones[Fred]}" /> 

(其中myns是一个XAML命名空间定义指向Fred对象在装配)

但是该程序在启动时崩溃或烧坏,或者它抱怨找不到Fred作为当前数据上下文的属性。

回答

9

这是我您的问题的解释的工作版本:

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和WilmaControl.xaml.cs未修改(只是的InitializeComponent();在构造函数中)。

而且截图:

enter image description here

+0

这是复杂的绑定语法的一个非常明显的例子。我不知道智能感知是否可以在您输入时使用? – 2014-10-07 21:47:35

+0

失踪}是在现场..我真的没有类叫弗雷德和威尔玛 – 2014-10-07 21:55:25

+0

@PeterM - 我会从答案中删除该部分。我想我应该意识到这一点。当然你会知道绑定语法。 – 2014-10-07 22:01:28