首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何动态绑定具有包含多个具有不同ItemSource的ItemTemplateSelector的ListView的ItemTemplateSelector?

如何动态绑定具有包含多个具有不同ItemSource的ItemTemplateSelector的ListView的ItemTemplateSelector?
EN

Stack Overflow用户
提问于 2019-11-05 10:44:58
回答 1查看 148关注 0票数 3

我有一个ItemTemplateSelector,它包含多个DataTemplates,其中有不同的DataTypes。因此,我有多个基于模块选择的ItemSources。如何根据选定的模块将我的ListView与多个ItemSources绑定?

解释:

1)当我选择模块A时,ViewModel_A是我的ItemSource,DataTemplateA是我的DataTemplate

2)当我选择B模块时,ViewModel_B是我的ItemSource,DataTemplateB是我的DataTemplate

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

如何动态选择我的ItemSource?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-05 12:03:25

第一步

首先,创建一个UserControl,其中包含Xaml中的ListView,以及用于ItemSourceDataTemplate的两个DependancyProperty

DataList.Xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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
}

第二步

现在,您可以使用任意多个DataTemplate和多个itemsource来控制这个用户控件,如下所示

MainPage.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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; }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58709680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档