views:

102

answers:

1

I would like to be able to create a DataTemplate to be used when a collection is passed into a control.

I am building a single control, that when passed an object, or a collection of objects, the view of the user control conforms to the template defined for the object type.

for example, this is a user control I have, that switches views when an object is passed into the .Content property.

<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>

I would like to be able to do something like

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

but I always get a compiler error, so I am at somewhat of a loss.

+1  A: 

You can create a typed collection and use that type instead of the IEnumerable directly

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

 <DataTemplate DataType="{x:Type Entities:MyCollection}">
            <OMS_Main_Screen:MaxCustomerConfigWpf DataContext="{Binding}" />
 </DataTemplate >
Jobi Joy
+1, and the reason you can't do what the OP wanted is because WPF does not allow templating on interfaces.
sixlettervariables
Yea, thats where I have been leaning. I was just wondering if there was another method of attack here.
Russ