2014-10-07 124 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