如何动态绑定具有 ItemTemplateSelector 的 ListView 的 ItemSource,其中包含多个具有不同数据类型的 DataTemplates?

How to dynamically bind the ItemSource of a ListView which has ItemTemplateSelector containing Multiple DataTemplates which have Different DataTypes?

我有一个 ItemTemplateSelector,它包含多个具有不同数据类型的数据模板。 因此,我有多个基于 Module Selected 的 ItemSources。 如何根据所选模块将我的 ListView 与多个 ItemSources 绑定?

解释:

1)ViewModel_A 是我的 ItemSource 和 DataTemplateA 是我的 DataTemplate 当我的模块 A 是 Selected

2)ViewModel_B 是我的 ItemSource DataTemplateB 是我的 DataTemplate 当我的模块 B 是 Selected

我尝试实现 BaseViewModel 并尝试在我的 ItemSource 中绑定 BaseViewModel 类型但这不允许访问派生的 class 属性。

如何动态地 Select 我的 ItemSource?

第 1 步

首先创建一个 UserControl,其中包含 Xaml 中的 ListView 以及 ItemSourceDataTemplate[=19 中的两个 DependancyProperty =]

DataList.Xaml

<UserControl
x:Class="MultipleDataTemplate.DataList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>

   <Grid>
   <ListView ItemsSource="{x:Bind ItemsSource,Mode=OneWay}"></ListView>
   </Grid>
</UserControl>

DataList.xaml.cs

public sealed partial class DataList : UserControl
{
    public DataList()
    {
        this.InitializeComponent();
    }
    #region ItemsSource
    public object ItemsSource
    {
        get { return (object)GetValue(ItemsSourceProperty); }
        set {  SetValue(ItemsSourceProperty, value); }
    }              
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(nameof(ItemsSource), typeof(object), typeof(DataList), new PropertyMetadata(null));
    #endregion
    #region ItemTemplate
    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

    public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register(nameof(ItemTemplate), typeof(DataTemplate), typeof(DataList), new PropertyMetadata(null));
    #endregion
}

第 2 步

现在您可以将此用户控件与任何多个数据模板和多个项目源一起使用,如下所示

MainPage.xaml

<Page
    x:Class="MultipleDataTemplate.Cars"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:controls="using:MultipleDataTemplate">
    <Page.Resources>
        <DataTemplate x:Key="CarKey" x:DataType="controls:Car">
            <Grid>
                <TextBlock Text="{x:Bind carprop1}"></TextBlock>
                <TextBlock Text="{x:Bind carprop2}"></TextBlock>

            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="BikeKey" x:DataType="controls:Bike">
            <Grid>
                <TextBlock Text="{x:Bind Bikeprop1}"></TextBlock>
                <TextBlock Text="{x:Bind Bikeprop2}"></TextBlock>

            </Grid>
        </DataTemplate>
    </Page.Resources>
    <Grid>        
        <controls:DataList ItemsSource="{x:Bind ItemSource,Mode=OneWay}" ItemTemplate="{x:Bind ItemTemplate}"></controls:DataList>
        <StackPanel>
            <Button Content="Cars" Click="CarsClick"/>
            <Button Content="Bike" Click="BikeClick"/>
        </StackPanel>
    </Grid>
</Page>

MainPage.xaml.cs

public sealed partial class Cars : Page, INotifyPropertyChanged
{
    public object _ItemSource { get; set; }

    public object ItemSource
    {
        get { return _ItemSource; }
        set
        {
            _ItemSource = value;
            this.OnPropertyChanged();
        }
    }
    public DataTemplate _itemTemplate { get; set; }

    public DataTemplate ItemTemplate
    {
        get { return _itemTemplate; }
        set
        {
            _itemTemplate = value;
            this.OnPropertyChanged();
        }
    }
    public Cars()
    {
        this.InitializeComponent();
    }
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;


    private void CarsClick(object sender, RoutedEventArgs e)
    {
        ItemSource = new List<Car>() { new Car() { carprop1 = "1", carprop2 = "2" } };
        ItemTemplate = this.Resources["CarKey"] as DataTemplate;
    }

    private void BikeClick(object sender, RoutedEventArgs e)
    {
        ItemSource = new List<Bike>() { new Bike() { Bikeprop1 = "1", Bikeprop2 = "2" } };
        ItemTemplate = this.Resources["BikeKey"] as DataTemplate;
    }
}
public class Car
{
    public string carprop1 { get; set; }
    public string carprop2 { get; set; }
}
public class Bike
{
    public string Bikeprop1 { get; set; }
    public string Bikeprop2 { get; set; }
}