开发者

How can I create a DataTemplate for a collection?

开发者 https://www.devze.com 2022-12-14 09:19 出处:网络
I would like to be able to create a DataTemplate to be used when a collection is passed into a control.

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:MaxCustomerConfigW开发者_运维知识库pf DataContext="{Binding}" />
</DataTemplate >

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


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

精彩评论

暂无评论...
验证码 换一张
取 消