c# - 如何将 listView ItemsSource 绑定到基本类型但传入特定类型
问题描述
我有一个具有 ListView 的自定义用户控件。我还有一个简单的对象层次结构,其中“组”“问题”等都从 BaseEntity 继承。
我正在尝试创建一个可以绑定到任何 BaseEntity 类型并显示其名称和 Id(基本类型属性)的用户控件。这样我就可以在整个应用程序中重复使用它来处理我想在控件中显示的任何 ObservableCollection 基本细节
当我绑定 BaseEntity 的 ObservableCollection 但不是特定类型时,它可以工作。除非我将依赖属性更改为特定类型 - 但这会破坏整个观点。
如何在 xaml 中绑定到基本类型?
所以我可以绑定以下集合:
ObservableCollection<Group>//inherits from BaseEntity
or
ObservableCollection<OtherType>//inherits from BaseEntity
or
ObservableCollection<BaseEntity> //its a BaseEntity
理想情况下,类似 {Binding Path=(BaseEntity)OwnedGroups} - 但它并不是那么简单
==================Control.xaml.cs==============
public partial class InOrOutControl : UserControl
{
public ObservableCollection<BaseEntity> EntitiesOwned
{
get { return (ObservableCollection<BaseEntity>)GetValue(EntitiesOwnedProperty); }
set { SetValue(EntitiesOwnedProperty, value); }
}
public static readonly DependencyProperty EntitiesOwnedProperty =
DependencyProperty.Register(nameof(EntitiesOwned), typeof(ObservableCollection<BaseEntity>), typeof(InOrOutControl), new PropertyMetadata(new ObservableCollection<BaseEntity>(), SetOwnedItemsSource));
private static void SetOwnedItemsSource(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
InOrOutControl controll = d as InOrOutControl;
if (controll != null)
{
controll.OutItemsLV.ItemsSource = e.NewValue as ObservableCollection<BaseEntity>;
}
}
public InOrOutControl()
{
InitializeComponent();
}
}
=================控制xaml=================
<UserControl x:Class="HonorsProject.View.CustomControlls.InOrOutControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:HonorsProject.View.CustomControlls"
xmlns:appCore="clr-namespace:HonorsProject.Model.Core;assembly=HonorsProject.Model"
mc:Ignorable="d"
Name="InOrOutCtrl"
d:DesignHeight="450" d:DesignWidth="800">
<ListView Name="OutItemsLV"
ItemsSource="{Binding EntitiesOwned, ElementName=InOrOutCtrl}"
Grid.Row="1"
Height="100"
Grid.Column="0">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
==============页面中的控件===============
<uc:InOrOutControl x:Name="InOrOutControl"
EntitiesOwned="{Binding Path=OwnedGroups,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
===============基本类型========
public class BaseEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
=============具体类型========
public class Group : BaseEntity
{
public virtual List<Student> Students { get; set; }
public int SomeSpecificProperty {get;set;}
public Group()
{
Students = new List<Student>();
}
}
解决方案
推荐阅读
- python - 函数可以直接在python中的字典中执行,而不是只在字典中调用函数吗?
- ios - 如何在 SwiftUI 中以交互方式按年份过滤日期列表
- ios - pdf文件未使用目标c显示ios中uiwebview中的所有页面
- c# - C# 存储库模式?复杂的查询仍然放在存储库中?
- vue.js - 在 Vuetify.js 插件中使用 vuex 状态
- system-verilog - 如何使随机重做直到得到我想要的数字?
- spring-boot - 在 Spring Boot 2.3 中找不到 javax.validation
- node.js - Mongoose findOne 查询未定义的值(数据库中不存在)。我正在使用 MERN 进行 Instagram 克隆
- unity3d - Unity 一个 2D 自上而下的游戏需要多少个角色绑定精灵层次结构以及如何过渡?
- java - 自动 .jar 创建