c# - UWP ListView的SelectedItem在ListView在其他ListView大数据时被重置
问题描述
当大ListView 的DataTemplate 包含小ListView 时,小ListView 的SelectedItem 会被重置。
我制作了示例应用程序,从三个选项中选择每个人的国家。我从后面的代码中选择了最后一个小 ListView 项目(索引:999)的“印度”。
但是当我向下滚动时,没有选择任何内容,调试日志如下。
[调试日志]
添加:印度,删除:
添加:,删除:印度
[主页.xaml]
<ListView ItemsSource="{x:Bind People}" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Person">
<StackPanel BorderBrush="Black" BorderThickness="0,0,0,1">
<TextBlock Text="{x:Bind Country, Mode=OneWay}"/>
<ListView x:Name="CountryList"
ItemsSource="{x:Bind Countries, Mode=OneWay}"
SelectedItem="{x:Bind Country, Mode=TwoWay}"
SelectionChanged="CountryList_SelectionChanged">
</ListView>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
[MainPage.xaml.cs]
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
GenerateSampleData();
}
private void GenerateSampleData()
{
for (int i = 0; i < 1000; i++)
{
People.Add(new Person
{
Countries = new List<string> { "China", "India", "USA" },
});
}
People[999].Country = "India";
}
private void CountryList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
StringBuilder sb = new StringBuilder("Added:");
foreach (var added in e.AddedItems)
{
if (added is string country)
{
sb.Append($" {country,-5}");
}
}
sb.Append(", Removed:");
foreach (var removed in e.RemovedItems)
{
if (removed is string country)
{
sb.Append($" {country,-5}");
}
}
Debug.WriteLine(sb);
}
完整来源如下。
https://github.com/tokane888/ListViewQuestion
而且我不确定它是否相关,但一些小的 ListView 显示为灰色,如下所示。
(即使是通过代码或手动操作,我也没有接触过这个小 ListView。)
解决方案
ListView
您的项目未被选中的原因是您在ListView
实际加载之前设置了您的属性。最好在ListView
Loaded
事件中设置选定的项目。您看到的灰色区域是由于
ListView
虚拟化,因为您ListView
包含大量数据。
为了解决这两个问题,请修改您的代码如下:
XAML:
<ListView ItemsSource="{x:Bind People}" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Person">
<StackPanel BorderBrush="Black" BorderThickness="0,0,0,1">
<TextBlock Text="{x:Bind Country, Mode=OneWay}"/>
<ListView x:Name="CountryList"
ItemsSource="{x:Bind Countries, Mode=OneWay}"
SelectedItem="{x:Bind Country, Mode=TwoWay}"
SelectionChanged="CountryList_SelectionChanged"
Loaded="CountryList_Loaded"
>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
后面的代码:
public sealed partial class MainPage : Page
{
public ObservableCollection<Person> People { get; } = new ObservableCollection<Person>();
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
GenerateSampleData();
}
private void GenerateSampleData()
{
for (int i = 0; i < 1000; i++)
{
People.Add(new Person
{
Countries = new List<string> { "China", "India", "USA" },
});
}
}
private void CountryList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
StringBuilder sb = new StringBuilder("Added:");
foreach (var added in e.AddedItems)
{
if (added is string country)
{
sb.Append($" {country,-5}");
}
}
sb.Append(", Removed:");
foreach (var removed in e.RemovedItems)
{
if (removed is string country)
{
sb.Append($" {country,-5}");
}
}
Debug.WriteLine(sb);
}
private void CountryList_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
People[999].Country = "India";
}
}
另请参阅此帖子以获取详细信息。
推荐阅读
- jquery - 将2个内容分别加载到2个div中jquery
- asp.net-mvc - 日期未从视图传递到控制器
- android - 使用视图寻呼机时,recyclerView 上更新的项目会丢失
- maven - 未找到 Maven 安装插件
- sql - 无法打开 lib '/usr/local/lib/libmsodbcsql.17.dylib'
- python - 测试视图集中的细节操作
- sql - SQL 作业执行成功,但在日志中我可以看到错误代码的描述:0xC001000E
- android - x秒后Android Webview重定向
- docker - Cassandra 无法在 Docker Toolbox 外部卷上启动
- python - 索引超出了 iterrows 的范围