這是一個類似於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
作爲當前數據上下文的屬性。
這是複雜的綁定語法的一個非常明顯的例子。我不知道智能感知是否可以在您輸入時使用? – 2014-10-07 21:47:35
失蹤}是在現場..我真的沒有類叫弗雷德和威爾瑪 – 2014-10-07 21:55:25
@PeterM - 我會從答案中刪除該部分。我想我應該意識到這一點。當然你會知道綁定語法。 – 2014-10-07 22:01:28