gpt4 book ai didi

c# - 如何获取被点击的 ListView 项目

转载 作者:太空宇宙 更新时间:2023-11-03 20:02:31 26 4
gpt4 key购买 nike

我无法在 ListView 中获取点击的项目

主页.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

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"));
}
}

编辑**

在进一步的测试中,我发现当事件触发时 sendere 都为空?

最佳答案

好的,这终于解决了。使用 SelectionChanged 事件,并通过 x:Name

为每个 ListViewItem 分配一个名称

主页.xaml

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

MainPage.xaml.cs

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

关于c# - 如何获取被点击的 ListView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26452475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com