【发布时间】:2018-12-27 22:12:08
【问题描述】:
Xaml 代码:
<Page
x:Class="DemoTestApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DemoTestApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
xmlns:vm="using:ViewModels"
>
<Page.DataContext>
<vm:TestViewModel/>
</Page.DataContext>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView Name="HeaderList" ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Border Background="Bisque">
<TextBlock Text="{Binding Name}"/>
</Border>
<ListView Name="SubItemsList">
<x:String>SubItem 1</x:String>
<x:String>SubItem 2</x:String>
<x:String>SubItem 3</x:String>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
ViewModel 代码:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ViewModels
{
class TestViewModel
{
public class Item
{
public string Test { get; set; }
}
public ObservableCollection<Item> Items { get; set; }
public TestViewModel()
{
Items = new ObservableCollection<Item>();
Items.Add(new Item { Test = "Item 1" });
Items.Add(new Item { Test = "Item 2" });
Items.Add(new Item { Test = "Item 3" });
Items.Add(new Item { Test = "Item 4" });
Items.Add(new Item { Test = "Item 5" });
Items.Add(new Item { Test = "Item 7" });
Items.Add(new Item { Test = "Item 8" });
Items.Add(new Item { Test = "Item 9" });
Items.Add(new Item { Test = "Item 10" });
Items.Add(new Item { Test = "Item 11" });
}
}
}
这是演示我的问题的演示代码。
当我单击 SubItemsList 上的任何项目时,我希望将事件绑定到视图模型中的命令,我该如何实现?
感谢阅读。
我已经查看了 stackoverflow 中的两个资源,但没有一个有用。
仅供参考,我是 UWP 的新手,对 MVVM 更是如此。
【问题讨论】:
标签: c# mvvm uwp mvvm-light listviewitem