Gridview 项目未更新

Gidview item not updating

我的页面有城市列表和搜索功能。第一次加载页面时,显示所有记录。

当用户输入搜索文本并点击搜索按钮时。它没有更新 gridview 列表。我通过放置调试点来检查我的代码是否工作正常。但是 gridview 列表没有显示更新的列表。

以下是我的代码。

XAML:

                                <StackPanel VerticalAlignment="Top">
                                    <TextBlock Style="{StaticResource ListTextBlockStyle}" FontWeight="Bold" Text="{Binding Description}" />
                                    <TextBlock Style="{StaticResource ListTextBlockStyle}" Text="{Binding Description}" />
                                </StackPanel>

                                <TextBlock Style="{StaticResource DistanceTextBlockStyle}" TextWrapping="Wrap" Text="XXXm" />
                                <Image Width="10" VerticalAlignment="Center" Source="ms-appx:///Assets/arrowright.png"/>
                            </StackPanel>
                            <Rectangle Height="1" Stroke="Black" StrokeThickness="0.5" Margin="0,3,0,0" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>

        <Border Grid.Row="1" Height="60" VerticalAlignment="Bottom">
            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <TextBox x:Name="txtSearch" Margin="0,0,10,0" TextWrapping="Wrap" PlaceholderText="Search" VerticalAlignment="Center" Width="300" Height="50" />
                    <Image x:Name="imgSearch" Height="50" Width="50" Source="ms-appx:///Assets/btnSearch.png" Tapped="imgSearch_Tapped"/>
                </StackPanel>
            </StackPanel>
        </Border>

C#:

   public List<City> gs_CityList = new List<City>();

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        fillCityList();
    }

    private void fillCityList()
    {
        gs_CityList.Clear();
        if (string.IsNullOrEmpty(CityListManagerManager.ms_searchTxt))
        {
            foreach (City foCity in CityListManagerManager.ms_CityList)
            {
                City loCity = new City();
                loCity.Description = foCity.Description.Replace("\n", "").Substring(0, 15) + "...";
                loCity.longtitude = foCity.longtitude;
                loCity.latitude = foCity.latitude;
                loCity.Location = foCity.Location;
                gs_CityList.Add(loCity);
            }
        }
        else
        {
            foreach (City foCity in CityListManagerManager.ms_CityList.Where(p => p.Description.ToLower().Contains(CityListManagerManager.ms_searchTxt.ToLower())))
            {
                City loCity = new City();
                loCity.Description = foCity.Description.Replace("\n", "").Substring(0, 15) + "...";
                loCity.longtitude = foCity.longtitude;
                loCity.latitude = foCity.latitude;
                loCity.Location = foCity.Location;
                gs_CityList.Add(loAEDPin);
            }

            txtSearch.Text = CityListManagerManager.ms_searchTxt;
        }

        if (gs_CityList.Count > 0)
        {
            gvCityList.DataContext = gs_CityList;       // --- This binding data to gridview
        }
        else
            MessageBox("City not found...!");
    }

    private void imgSearch_Tapped(object sender, TappedRoutedEventArgs e)
    {
        CityListManagerManager.ms_searchTxt = txtSearch.Text.Trim();
        fillCityList();
    }

您应该尝试将 List<City> 更改为 ObservableCollection<City>,因为这样可以让绑定收到有关更改的通知。

您也可以考虑使用 CollectionViewSource 作为 GridView 的数据源并修改其 Filter 属性 而不是重新填充集合。