我有这个列表视图:
<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 中。那可能吗?我知道对于子项列表项,数据是一个项目,它只是一个数据模型,它不包含任何单击处理程序。但是,如何捕获视图模型中的点击而不是隐藏代码中的点击?
我还上传了一张图片来形象化我想要的东西:
你真正需要的是一个ClickCommand
在你的视图模型中。但自从ListView
控件不暴露ItemClickCommand
,一种常见的方法是使用behavior建立您之间的连接ClickCommand
和ItemClick
event.
您正在寻找的这种特殊行为称为InvokeCommandAction
,可以在这个中找到努吉特包 https://www.nuget.org/packages/Microsoft.Xaml.Behaviors.Uwp.Managed/.
基本上最终的结果会是这样的 -
<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)