UWP:如何从视图模型中的另一个列表视图而不是代码隐藏中捕获列表视图部分的点击?

UWP: How to catch the click of a listview part from another listview in the viewmodel instead of code-behind?

我有这个列表视图:

<Page
    x:Class="DataBase.ControlsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding CustomersViewModel, Source={StaticResource ViewModelLocator}}">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView Name="HeaderList" ItemsSource="{Binding Customers}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Border Background="Bisque">
                            <TextBlock Text="{Binding Name"/>
                        </Border>
                        <ListView Name="SubItemsList" ItemsSource="{Binding Project}" ItemClick="SubItemClick">
                        <x:String>SubItem 1</x:String>
                        <x:String>SubItem 2</x:String>
                        <x:String>SubItem 3</x:String>
                        </ListView>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        <x:String>Item 1</x:String>
        <x:String>Item 2</x:String>
        <x:String>Item 3</x:String>
        </ListView>
    </Grid>
</Page>

我只想在我的 CustomersViewModel 中捕获子项 (ItemClick="SubItemClick">) 的点击。那可能吗?我知道对于子项列表项,数据是一个项目,它只是一个数据模型,它不包含任何点击处理程序。但是,我怎样才能在视图模型中而不是在代码隐藏中捕捉到点击?

另外我正在上传一张图片来可视化我想要的东西:

您真正需要的是视图模型中的 ClickCommand。但是由于 ListView 控件不公开 ItemClickCommand,一种常见的方法是使用 行为 来建立 ClickCommandItemClick 事件。

您正在寻找的这种特定行为称为 InvokeCommandAction,可以在 nuget package.

中找到它

基本上最终结果应该是这样的 -

<ListView Name="HeaderList" ItemsSource="{Binding Customers}">
    <Interactivity:Interaction.Behaviors>
        <Interactions:EventTriggerBehavior EventName="ItemClick" SourceObject="{Binding ElementName=HeaderList}">
            <Interactions:InvokeCommandAction Command="{Binding ClickCommand}"/>
        <Interactions:EventTriggerBehavior>
    <Interactivity:Interaction.Behaviors>

正如 Justin XL 所说,我一直在寻找的答案是: ItemClick="{Binding DataContext.ClickCommand, ElementName=HeaderList}"