- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 ItemTemplateSelector,它包含多个具有不同数据类型的数据模板。因此,我有多个基于所选模块的 ItemSources。如何根据所选模块将我的 ListView 与多个 ItemSources 绑定(bind)?
解释:
1)ViewModel_A 是我的 ItemSource,DataTemplateA 是我的 DataTemplate,当我的模块 A 被选中时
2)ViewModel_B 是我的 ItemSource DataTemplateB 是我的 DataTemplate 当我的模块 B 被选中时
我尝试实现一个 BaseViewModel 并尝试在我的 ItemSource 中绑定(bind) BaseViewModel 类型但这不允许访问派生类属性。
如何动态选择我的ItemSource?
最佳答案
首先创建一个 UserControl
,它包含你的 Xaml 中的 ListView
和两个 DependancyProperty
用于 ItemSource
和 数据模板
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
}
现在您可以使用以下任何多个数据模板和多个项目源来控制此用户
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; }
}
关于c# - 如何动态绑定(bind)具有 ItemTemplateSelector 的 ListView 的 ItemSource,其中包含多个具有不同数据类型的 DataTemplates?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58709680/
ItemTemplateSelector 应用于对象集合 - 例如 ItemsControl。 单项控件是否具有相同的功能?我想选择一个考虑到对象内部值的数据模板。我可以为列表执行此操作,但不能为单个
我正在尝试在 WPF(MVVM 方法)中创建一个简单的 2 级 TreeView 。对于我的第一个级别,我有一个标准的数据模板,对于我的第二个级别,我想使用一个模板选择器,以便我可以根据其属性之一更改
在我的应用程序中,每次 treeView 加载它的项目时,我都会收到此错误。此错误使我的应用程序加载速度变慢,并且至少需要两分钟才能加载。 错误是:System.Windows.Data Error:
在 WPF 项目中,我有一个 ComboBox 可以从不同的对象中进行选择。使用 ItemsControl 和它的 ItemTemplateSelector 我试图根据对象的属性为 ComboBox
我有一个 ListView,我在后面的代码中以编程方式设置了它的 ItemsSource。以前我只有一个模板,现在我想使用多个模板,根据我添加到ListView的项目来选择模板。 这是我的 XAML
我有一个 GridView 作为我在 SemanticZoom 控件中的缩小 View 。此 GridView 使用自定义 DataTemplateSelector 作为 ItemTemplateSe
我很久以前做过一点 WPF 编程,但我刚刚回到使用 UWP 的 xaml,但我认为这应该可行,但无法弄清楚为什么。基本上我想使用 ItemsControl(因为我只想列出一些数据,我不想选择)而不是
在我们的数据网格中,我们使用 ItemTemplateSelector 根据绑定(bind)到特定单元格的数据在两个数据模板之间切换。 由于列数取决于我们在 DataGrid 中使用的 AutoGen
假设以下 XAML 定义的窗口:
我有一个 ItemTemplateSelector,它包含多个具有不同数据类型的数据模板。因此,我有多个基于所选模块的 ItemSources。如何根据所选模块将我的 ListView 与多个 Ite
我是一名优秀的程序员,十分优秀!