2009-12-21 68 views
0

我希望能夠創建一個DataTemplate集合傳遞到控件時使用。如何爲集合創建DataTemplate?

我正在構建一個控件,當傳遞一個對象或一組對象時,用戶控件的視圖符合爲該對象類型定義的模板。

例如,這是我擁有的用戶控件,它在對象傳遞到.Content屬性時切換視圖。

<UserControl x:Class="Russound.Windows.UI.UserControls.MAX.OMS_Main_Screen.OMSContextSwitcher" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:Entities="clr-namespace:Russound.Components.ReturnAuthorization.Entities;assembly=Russound.Components" 
      xmlns:Return_Authorization="clr-namespace:Russound.Windows.UI.UserControls.Return_Authorization" 
      xmlns:CallLog="clr-namespace:Russound.Windows.UI.UserControls.CallLog" 
      xmlns:Entities1="clr-namespace:Russound.Components.Membership.Entities;assembly=Russound.Components" 
      xmlns:Membership="clr-namespace:Russound.Windows.UI.UserControls.Membership" 
      xmlns:Entities2="clr-namespace:Russound.Components.Commerce.MAX.Entities;assembly=Russound.Components" 
      xmlns:OMS_Main_Screen="clr-namespace:Russound.Windows.UI.UserControls.MAX.OMS_Main_Screen" 
      xmlns:Entities3="clr-namespace:Russound.Components.CallLog.Entities;assembly=Russound.Components" 
      MinHeight="600" MinWidth="700"> 

    <UserControl.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="/Russound.Windows;component/UI/RussoundDictionary.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 

       <DataTemplate DataType="{x:Type Entities3:Case}" > 
        <CallLog:CaseReadOnlyDisplay DataContext="{Binding}" /> 
       </DataTemplate> 

       <DataTemplate DataType="{x:Type Entities:RAMaster}"> 
        <Return_Authorization:RaMasterUi DataContext="{Binding}" /> 
       </DataTemplate> 

       <DataTemplate DataType="{x:Type Entities1:RussUser}"> 
        <Membership:CMCControlWpf DataContext="{Binding}" /> 
       </DataTemplate > 

       <DataTemplate DataType="{x:Type Entities2:MaxCustomer}"> 
        <OMS_Main_Screen:MaxCustomerConfigWpf DataContext="{Binding}" /> 
       </DataTemplate > 


     </ResourceDictionary> 
    </UserControl.Resources> 
</UserControl> 

我希望能夠做這樣的事情:

<DataTemplate DataType="{x:Type IEnumerable<MaxCustomer>}"> 
    <OMS_Main_Screen:MaxCustomerConfigWpf DataContext="{Binding}" /> 
</DataTemplate > 

,但我總是得到一個編譯器錯誤,所以我在有點茫然的。

回答

3

您可以創建一個類型集合,並直接

public class MyCollection:IEnumerable<MaxCustomer> 
{ 
    .... 
} 

<DataTemplate DataType="{x:Type Entities:MyCollection}"> 
      <OMS_Main_Screen:MaxCustomerConfigWpf DataContext="{Binding}" /> 
</DataTemplate > 
+2

+1使用該類型代替了IEnumerable的,你不能做OP想要的東西的原因是因爲WPF不允許在接口模板。 – user7116 2009-12-21 18:58:46

+0

是的,那是我一直在傾斜。我只是想知道這裏是否有另一種攻擊方法。 – Russ 2009-12-21 19:35:24

相關問題