[英]How to determine which child element of a ListView Item was clicked?
I'm developing a windows phone 8.1 app in XAML and C#. 我正在用XAML和C#开发Windows Phone 8.1应用程序。 I have a ListView getting its Items from a bound list and displaying them through a DataTemplate. 我有一个ListView从绑定列表中获取其Items并通过DataTemplate显示它们。 Now, in this DataTemplate there are multiple child elements, and when the user taps on an item in the list, I want to be able to determine what child element he actually touched. 现在,在此DataTemplate中有多个子元素,并且当用户点击列表中的一个项目时,我希望能够确定他实际触摸过的子元素。 Depending on that, the app should either expand a view with more details inside the Item, or navigate to another page. 取决于此,该应用程序应在“项目”内展开具有更多详细信息的视图,或导航到另一个页面。
The ItemClick event handler of the ListView is ListView_ItemClick(object sender, ItemClickEventArgs e), and I thought e.OriginalSource would maybe give me the answer, but this just gave me the clicked ListItem. ListView的ItemClick事件处理程序是ListView_ItemClick(对象发送者,ItemClickEventArgs e),我认为e.OriginalSource可能会给我答案,但这只是给我单击的ListItem。
I have yet to try if encapsulating the children with buttons and intercepting their click events would work, but I'm happy to try any alternative there might be for this. 我还没有尝试过用按钮封装孩子并拦截他们的点击事件是否可行,但是我很乐意尝试任何可能的替代方法。
I just found the solution myself. 我自己找到了解决方案。 I set the ListView to SelectionMode="None" and IsItemClickEnabled="False", and then I added Tapped handlers for the individual child elements. 我将ListView设置为SelectionMode =“ None”和IsItemClickEnabled =“ False”,然后为各个子元素添加了Tapped处理程序。 Works just as I wanted. 就像我想要的那样工作。
I've got a TextBlock and an Image in one ListViewItem and have just used the Image_PointerPressed event. 我在一个ListViewItem中有一个TextBlock和一个Image,并且刚刚使用了Image_PointerPressed事件。 Doing that also fires the ItemClick event for the ListView so I disable it first, do the stuff I want, then re-enable the ItemClick event so that still fires when the TextBlock is pressed. 这样做还会触发ListView的ItemClick事件,因此我首先将其禁用,执行所需的操作,然后重新启用ItemClick事件,以便在按下TextBlock时仍然触发。
Code behind: 后面的代码:
private async void imgDone_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// disable click event so it won't fire as well
lvwCouncils.IsItemClickEnabled = false;
// do stuff
MessageDialog m = new MessageDialog("User Details");
await m.ShowAsync();
// Re-enable the click event
lvwCouncils.IsItemClickEnabled = true;
}
Xaml: XAML:
<ListView x:Name="lvwCouncils" ItemClick="lvwCouncils_ItemClicked" IsItemClickEnabled="true" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock
Grid.Column="1"
Text="{Binding council_name}"
FontSize="24"
Margin="10,10,30,10"
/>
<Border Height="20" Width="20" Margin="10,10,0,10" >
<Image x:Name="imgDone"
Source="Assets/user_earth.png" Stretch="UniformToFill" PointerPressed="imgDone_PointerPressed"/>
</Border>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Use the SelectionChanged
event. 使用SelectionChanged
事件。
Cast the sender
object to ListView
type and then retrieve the item from the SelectedItem
property. 将sender
对象强制转换为ListView
类型,然后从SelectedItem
属性中检索该项目。
Similar question here but for a different control : 这里有类似的问题,但有不同的控制方法:
Get the index of the selected item in longlistselector 在longlistselector中获取所选项目的索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.