简体   繁体   English

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

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

I have an ItemTemplateSelector which contains Multiple DataTemplates which have Different DataTypes.我有一个 ItemTemplateSelector,其中包含多个具有不同数据类型的数据模板。 I thus have multiple ItemSources based on Module Selected.因此,我有多个基于 Module Selected 的 ItemSource。 How to bind my ListView with multiple ItemSources based on the module selected?如何根据所选模块将我的 ListView 与多个 ItemSource 绑定?

Explanation:解释:

1)ViewModel_A is my ItemSource and DataTemplateA is my DataTemplate when my Module A is Selected 1)ViewModel_A 是我的 ItemSource 和 DataTemplateA 是我的 DataTemplate 当我的模块 A 被选择

2)ViewModel_B is my ItemSource DataTemplateB is my DataTemplate when my Module B is Selected 2)ViewModel_B 是我的 ItemSource DataTemplateB 是我的 DataTemplate 当我的模块 B 被选中时

I tried Implementing a BaseViewModel and tried binding the BaseViewModel Type in my ItemSource But this doesn't allow the access of derived class properties.我尝试实现 BaseViewModel 并尝试在我的 ItemSource 中绑定 BaseViewModel 类型,但这不允许访问派生的 class 属性。

How to Dynamically Select My ItemSource?如何动态 Select 我的 ItemSource?

Step 1步骤1

First Create a UserControl which contains your ListView in your Xaml and two DependancyProperty for ItemSource and DataTemplate首先创建一个UserControl ,其中包含 Xaml 中的ListView以及ItemSourceDataTemplate的两个DependancyProperty

DataList.Xaml数据列表.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数据列表.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
}

Step 2第2步

Now you can you this usercontrol with any multiple DataTemplate's and multiple itemsource as below现在,您可以使用任何多个 DataTemplate 和多个 itemsource 来使用此用户控件,如下所示

MainPage.xaml主页.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主页.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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在UWP ListView中具有多个数据模板(x:bind) - How to have multiple datatemplates in UWP ListView (x:bind) 如何将ListView控件绑定到ComboBox ItemSource - How to bind ListView controls to ComboBox ItemSource WPF在DataTemplates中改进了ItemTemplateSelector - WPF emended ItemTemplateSelector in a DataTemplates 如何重新绑定已经与数据表绑定的Listview - How to Rebind Listview which is already bind with a datatable 将ListView ItemSource绑定到视图(MVVM)中的两个不同的GetSet属性 - Bind a ListView ItemSource to two different GetSet properties in the View (MVVM) WPF MVVM:在子ItemSource的ViewModel上绑定命令 - WPF MVVM : Bind a command which is on a ViewModel in a Child ItemSource 如何将itemsource(字符串数组)的值绑定到ListView中的标签 - How to bind the values of the itemsource (array of strings) to a label in a ListView 将ListView与包含UserControls的DataTemplates绑定到MVVM中的ViewModels - Binding ListView with DataTemplates containing UserControls to ViewModels in MVVM 试图在已经有itemsource的datagrid中添加一个组合框 - Trying to add a combobox within datagrid which has an itemsource already 如何在位于 Bindable StackLayouts 内的 ItemTemplateSelector 中引用父 DataTemplates 参数? - How do you reference a parent DataTemplates parameter inside an ItemTemplateSelector located inside of a Bindable StackLayouts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM