简体   繁体   English

如何获得单击的ListView项

[英]How to Get Clicked ListView Item

I am having trouble getting the clicked item in a ListView 我在获取ListView的单击项时遇到麻烦

MainPage.xaml MainPage.xaml

 <ListView 
                    ReorderMode="Disabled"
                    SelectionMode="Single"
                    IsItemClickEnabled="True"
                    ItemClick="Section_ItemClick"
                    ContinuumNavigationTransitionInfo.ExitElementContainer="True">

                    <ListViewItem Margin="0,0,0,10" Background="LightGray">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="/Assets/Icons/1.png" Width="94" Height="94"/>
                            <TextBlock x:Uid="1" />
                        </StackPanel>
                    </ListViewItem>

                    ...
</ListView>

MainPage.xaml.cs MainPage.xaml.cs

private async void Section_ItemClick(object sender, ItemClickEventArgs e)
    {
        ListViewItem itemId = ((sender as ListView).SelectedItem as ListViewItem);

        if (itemId != null)
        {
            .. do something ..
        }
        else
        {
            throw new Exception(this.resourceLoader.GetString("NavigationFailedExceptionMessage"));
        }
    }

EDIT** 编辑**

In further testing I've discovered that both sender and e are null when the event fires? 在进一步的测试中,我发现事件触发时, sendere均为空?

Ok this solved it (finally). 好的,这终于解决了。 Use the SelectionChanged event, and assign each ListViewItem a name via x:Name 使用SelectionChanged事件,并通过x:Name每个ListViewItem分配一个x:Name

MainPage.xaml MainPage.xaml

<ListView 
                    x:Name="SettingsHub"
                    ReorderMode="Disabled"
                    SelectionChanged="SettingsHub_SelectionChanged"
                    ContinuumNavigationTransitionInfo.ExitElementContainer="True">
      ...
</ListView>

MainPage.xaml.cs MainPage.xaml.cs

private void SettingsHub_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            var itemId = (e.AddedItems[0] as ListViewItem).Name;
        }            
    }

Cast the ClickedItem first. 首先投放ClickedItem One of the following should work (I am not sure which one will work because I don't have windows phone SDK installed right now): 以下其中一项应该可以工作(我不确定哪一个可以工作,因为我现在没有安装Windows Phone SDK):

var itemId = (e.ClickedItem as ListViewItem);
var itemId = (e.ClickedItem as Item);

then check for null reference: 然后检查null引用:

if (itemId != null && itemId.IsSelected)
{ }

Also check the documentation for ItemCLickEventArgs 还要检查ItemCLickEventArgs的文档

this was a mod I used 这是我使用的一个mod

for a listView that has just stings in it. 一个只有刺痛的listView。 Im storing rss feed urls in a listview, hence the Method Name. 我在列表视图中存储rss feed URL,因此存储了方法名称。 Left debug in code to help. 在代码中进行调试以提供帮助。

    private void RSSItem_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine(" CALLED ITEM CHANGED ");

        //ListViewItem itemId = ((sender as ListView).SelectedItem as ListViewItem);
        int count = e.AddedItems.Count;
        string  itemStr = e.AddedItems[0].ToString();

        System.Diagnostics.Debug.WriteLine(" DOWNLOADING FEED URL : " + itemStr);

    }
    private void DrivingHistoryListView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var SelectedItem = (DrivingHistoryItem)e.ClickedItem;
        .....
    }
private void DrivingHistoryListView_ItemClick(object sender, ItemClickEventArgs e)
{
    var SelectedItem = (sender as ListView).SelectedItem;
    .....
}

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

相关问题 如何在ListView中获取单击的项目 - How to get clicked item in ListView 获取Clicked ListView项目属性 - Get Clicked ListView item attributes 如何获取右键单击打开上下文菜单之前的ListView项 - How to get the ListView Item that was right clicked before opening the context menu 单击行中的按钮时获取ListView项 - Get ListView item when button in row is clicked 如何使用listview.GetItemAtPosition(e.Position)获取用户单击的ListView项目的数据? - How to get the data of a ListView item the user has clicked on with listview.GetItemAtPosition(e.Position)? 如何获取没有列表视图项的列表视图的单击列的列索引? - How to get column index of clicked column of listview that hasn't listview item? C#:如何从ListIn控件的SelectedIndexChanged中单击实际项目? - C# : How do I get the actual item clicked from SelectedIndexChanged for a ListView control? 在我的ListView控件中,有没有办法让我获取单击项的索引? - In my ListView control, is there a way for me to GET the index of a clicked item? 在C#和WinRT中从IsItemClickEnabled ListView获得单击的项目 - get the clicked item from a IsItemClickEnabled ListView in C# and WinRT WPF在单击ContextMenu时获取ListView项的名称 - WPF Get name of ListView item when ContextMenu clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM