简体   繁体   English

GridView上的ItemClick事件

[英]ItemClick event on part of GridView

I have a collection of objects that I want to bind to my Xaml (using a GridView at the moment). 我有一个要绑定到Xaml的对象集合(此刻使用GridView)。 I don't know if a GridView is the best tool for what I want to achieve. 我不知道GridView是否是我想要实现的最佳工具。

This is roughly my current DataTemplate: 这大致是我当前的DataTemplate:

资料范本

I need a clicked/tapped trigger on Grid1. 我需要在Grid1上单击/点击触发器。 But when I use this template with a GridView, the ItemClick event will get triggered on the complete template - which is correct of course. 但是,当我将此模板与GridView一起使用时,ItemClick事件将在完整的模板上触发-当然是正确的。 How can I set an click event on Grid1? 如何在Grid1上设置单击事件? Can I add this event in my DataTemplate declaration? 我可以在DataTemplate声明中添加此事件吗?

That's basically the xaml for my GridView: 基本上就是GridView的xaml:

<GridView x:Name="RoomsGridView"
                    VerticalAlignment="Top"
                    ItemsSource="{Binding Rooms}"
                    ItemTemplate="{StaticResource RoomTemplate}"
                    SelectionMode="None"
                    IsSwipeEnabled="false" 
                    IsItemClickEnabled="True"
                    ItemClick="RoomsGridView_ItemClick" Padding="0">
                        <GridView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </GridView.ItemsPanel>                            
                    </GridView>

Edit : I've found this sample but this seems only to work with Buttons!? 编辑 :我已经找到了此示例,但这似乎只与按钮一起使用!

In the code you posted above you've told to GridView to capture item clicks via IsItemClickEnabled="True" . 在上面发布的代码中,您已告知GridView通过IsItemClickEnabled="True"捕获项目点击。 You cannot have that option enabled if you want to be able to click on elements inside the DataTemplate . 如果要能够单击DataTemplate元素,则不能启用该选项。

I would suggest that you disable IsItemClickEnabled and instead add a click handler to your Grid1. 我建议您禁用IsItemClickEnabled然后将单击处理程序添加到Grid1中。 Even better would be to substitute your Grid1 with a button that has custom styling. 更好的方法是将Grid1替换Grid1具有自定义样式的按钮。 This would further allow you to bind the button to a command that you can control from your view model. 这将进一步允许您将按钮绑定到可以从视图模型控制的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM