标识已通过事件处理程序窃听的ItemsControl项的索引/行号 [英] Identifying the index/row number of the ItemsControl item that has been tapped via an event handler

查看:70
本文介绍了标识已通过事件处理程序窃听的ItemsControl项的索引/行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户保存的位置列表,以供将来参考(这是针对气象应用程序的).

I've a list of locations the user has saved for future reference (this is for a weather app).

我正在使用ItemsControl来显示列表:

I'm using an ItemsControl to present the list:

            <ItemsControl 
                Grid.Row="0"
                x:Name="locations"
                ItemsSource="{Binding Path=Locations}"
                ItemTemplate="{StaticResource locationRow}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Vertical" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>

这是ItemTemplate:

This is the ItemTemplate:

            <DataTemplate 
                    x:Key="locationRow">

                    <StackPanel>
                        <TextBlock 
                            x:Name="City"
                            Tap="Location_Tap" 
                            Text="{Binding City}"/>

                        <TextBlock 
                            x:Name="Country"
                            Margin="0,0,0,20"
                            FontSize="20"
                            Tap="Location_Tap" 
                            Text="{Binding Country}"/>
                    </StackPanel>
                </DataTemplate>

一切正常.但是我在确定已点击的行/位置时遇到了问题.

Everything displays fine. But I'm having problems identifying the row/location that has been tapped.

我想做的是点击某个项目的任一TextBlock时,确定Location_Tap事件处理程序中该项目的行号,以便我确切地知道点击了哪个位置,从而显示被点击的天气地点.例如,如果被窃听物品的索引号是1,那么我知道被窃听位置的索引也是1.

What I'd like to do is when either TextBlock for an Item is tapped, to determine the row number of the Item in the Location_Tap event handler so that I know exactly what location was tapped and thereby show the weather for the tapped location. For example, if the tapped item index number is 1, then I know the index of the location tapped is also 1.

这里的任何指导将不胜感激.

Any guidance here would be much appreciated.

谢谢

Bardi

更新:我遵循了Pantelis的建议,并且几乎奏效.我会得到一个错误:

UPDATE: I followed Pantelis' suggestion and it almost worked. I would get an error:

无法将lambda表达式转换为类型'MetroApp.LocationDbRecord',因为它不是委托类型"

"Cannot convert lambda expression to type 'MetroApp.LocationDbRecord' because it is not a delegate type"

此错误将出现在事件处理程序的IndexOf方法的参数的第二行中:

This error would be in the event handler's second line for the argument of the IndexOf method:

        var sourceObject = ((FrameworkElement)sender).DataContext as LocationDbRecord;
        int index = this.viewModel.Locations.IndexOf(item => item.City == sourceObject.City);

不过,我仅通过将第二行更改为:

I managed to get the index I'm after however by changing the second line simply to:

int index = this.viewModel.Locations.IndexOf(sourceObject);

所以我得到了我想要的解决方案,但仍然很好奇为什么最初的建议1)不起作用,而2)是推荐的解决方案,而不仅仅是使用sourceObject作为参数值(我敢肯定有一个很好的理由!).

So I got the solution I'm after but am still curious why the original suggestion 1) didn't work and 2) was the recommended solution instead of just using the sourceObject as the parameter value (I'm sure there was a good reason!).

推荐答案

您无法通过索引找到UIElement(被轻击的项).您的ItemsPanel是不支持索引的StackPanel.相反,您可以在ItemsSource中获取被点击项目的DataContext及其索引,然后使用该对象.

You cannot locate the UIElement (Tapped Item) by index. Your ItemsPanel is a StackPanel which does not support indexing. Instead, you can get the DataContext of the tapped item and its index in your ItemsSource and work with that object.

private void TextBlock_OnTap(object sender, GestureEventArgs e)
{
    var sourceObject = ((FrameworkElement) sender).DataContext as TypeOfLocation;
    int index = ViewModel.Locations.IndexOf(item => item.City == sourceObject.City);
}

为了在View中获得ViewModel的实例,可以在代码隐藏类中设置ViewModel的公共属性.

In order to get an instance of your ViewModel in your View you can set a public property of your ViewModel in the code-behind class.

public MyViewModel ViewModel
{  
    get { return (MyViewModel) this.DataContext; }
}

这篇关于标识已通过事件处理程序窃听的ItemsControl项的索引/行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆