【问题标题】:UWP ListView's SelectedItem is reset when the ListView is in other ListView with large dataUWP ListView的SelectedItem在ListView在其他ListView大数据时被重置
【发布时间】: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。)

【问题讨论】:

    标签: c# listview uwp


    【解决方案1】:
    1. 您的ListView 项目未被选中的原因是您在实际加载ListView 之前设置了您的属性。最好在 ListView Loaded 事件中设置选中项。

    2. 您看到灰色区域是因为 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";
            }
        }
    

    另请参阅this 帖子了解详细信息。

    【讨论】:

    • 感谢您的帮助。 效果很好,现在没有显示灰色的 ListView。但是当我向下滚动到底部并再次上下滚动时,“印度”被取消选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 2017-01-24
    • 2016-11-12
    • 2016-03-18
    • 1970-01-01
    • 2015-05-29
    相关资源
    最近更新 更多