如何为集合创建数据模板?

发布于 2024-08-15 18:56:47 字数 2767 浏览 5 评论 0原文

我希望能够创建一个 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 >

但我总是收到编译器错误,所以我有点不知所措。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你到最终 2024-08-22 18:56:47

您可以创建一个类型化集合并直接使用该类型而不是 IEnumerable

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

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

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 >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文