首页 > 解决方案 > 如何将 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>();
    }
}

标签: c#wpf

解决方案


推荐阅读