【发布时间】:2019-02-19 00:11:16
【问题描述】:
当大ListView的DataTemplate包含小ListView时,小ListView的SelectedItem被重置。
我制作了示例应用程序,从三个选项中选择每个人的国家/地区。
我从后面的代码中最后选择了“印度”小 ListView 项目(索引:999)。
但是当我向下滚动时,没有选择任何内容,调试日志如下。
[调试日志]
已添加:印度,已删除:
已添加:,已删除:印度
[MainPage.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。)
【问题讨论】: