C# 如何动态绑定具有ItemTemplateSelector的ListView的ItemSource,该ItemTemplateSelector包含多个具有不同数据类型的DataTemplates?
我有一个ItemTemplateSelector,它包含多个具有不同数据类型的数据模板。 因此,基于所选模块,我有多个ItemSources。 如何根据所选模块将我的ListView与多个ItemSources绑定 说明: 1) 当选择我的模块A时,ViewModel_A是我的ItemSource,DataTemplateA是我的DataTemplate 2) ViewModel_B是我的ItemSource DataTemplateB是选中我的模块B时的我的DataTemplate 我尝试实现BaseViewModel并尝试在ItemSource中绑定BaseViewModel类型,但这不允许访问派生类属性 如何动态选择我的ItemSource?步骤1 首先创建一个C# 如何动态绑定具有ItemTemplateSelector的ListView的ItemSource,该ItemTemplateSelector包含多个具有不同数据类型的DataTemplates?,c#,uwp,uwp-xaml,datatemplateselector,itemsource,C#,Uwp,Uwp Xaml,Datatemplateselector,Itemsource,我有一个ItemTemplateSelector,它包含多个具有不同数据类型的数据模板。 因此,基于所选模块,我有多个ItemSources。 如何根据所选模块将我的ListView与多个ItemSources绑定 说明: 1) 当选择我的模块A时,ViewModel_A是我的ItemSource,DataTemplateA是我的DataTemplate 2) ViewModel_B是我的ItemSource DataTemplateB是选中我的模块B时的我的DataTemplate 我尝试实现
UserControl
,其中包含Xaml中的ListView
,以及ItemSource
和DataTemplate的两个DependencyProperty
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>
<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>
步骤2
现在,您可以使用任意多个DataTemplate和多个itemsource来创建此usercontrol,如下所示
MainPage.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>
<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 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
}
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; }
}
公共密封部分等级车辆:第页,INotifyPropertyChanged
{
公共对象_ItemSource{get;set;}
公共对象项源
{
获取{return\u ItemSource;}
设置
{
_ItemSource=值;
this.OnPropertyChanged();
}
}
公共数据模板_itemTemplate{get;set;}
公共数据模板ItemTemplate
{
获取{return\u itemTemplate;}
设置
{
_itemTemplate=值;
this.OnPropertyChanged();
}
}
公共汽车
{
this.InitializeComponent();
}
public void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
this.PropertyChanged(this,newpropertychangedventargs(propertyName));
}
公共事件属性更改事件处理程序属性更改;
专用无效车辆单击(对象发送者,路由目标e)
{
ItemSource=new List(){new Car(){carprop1=“1”,carprop2=“2”};
ItemTemplate=this.Resources[“CarKey”]作为DataTemplate;
}
私有void bikelick(对象发送方,路由目标)
{
ItemSource=new List(){new Bike(){Bikeprop1=“1”,Bikeprop2=“2”};
ItemTemplate=this.Resources[“BikeKey”]作为DataTemplate;
}
}
公车
{
公共字符串carprop1{get;set;}
公共字符串carprop2{get;set;}
}
公共级自行车
{
公共字符串Bikeprop1{get;set;}
公共字符串Bikeprop2{get;set;}
}
你能用2个项目来源的例子解释一下吗?当然,我会把它添加到解决方案中