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

时光毁灭记忆、已成空白 提交于 2019-12-04 18:10:01

What you really need is a ClickCommand in your viewmodel. But since the ListView control doesn't expose a ItemClickCommand, one common way is to use a behavior to establish the connection between your ClickCommand and the ItemClick event.

This particular behavior you are looking for is called InvokeCommandAction, which can be found in this nuget package.

Basically the end result would look something like this -

<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>

As Justin XL said, the answer I was looking for is this: ItemClick="{Binding DataContext.ClickCommand, ElementName=HeaderList}"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!