如何为集合创建数据模板?
我希望能够创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个类型化集合并直接使用该类型而不是 IEnumerable
You can create a typed collection and use that type instead of the IEnumerable directly