Xamarin 表单更新 listView itemSource [英] Xamarin form update listView itemSource
问题描述
好的,我有一个 ListView
对象,它有一个 List
作为 ItemSource
,我想刷新 ItemSource
每当对象列表发生变化时.ListView 有一个个性化的 ItemTemplate
现在我已经这样做了:
Ok I have a ListView
object which have a List<Filiale>
as ItemSource
and I'd like to refresh the ItemSource
whenever the list of object change. The ListView has a personalized ItemTemplate
For now I have done this:
public NearMe ()
{
list=jM.ReadData ();
listView.ItemsSource = list;
listView.ItemTemplate = new DataTemplate(typeof(FilialeCell));
searchBar = new SearchBar {
Placeholder="Search"
};
searchBar.TextChanged += (sender, e) => {
TextChanged(searchBar.Text);
};
var stack = new StackLayout { Spacing = 0 };
stack.Children.Add (searchBar);
stack.Children.Add (listView);
Content = stack;
}
public void TextChanged(String text){
//DOSOMETHING
list=newList;
}
正如您在 TextChanged 方法中看到的,我为前一个列表分配了一个新列表,但视图中没有任何变化.在我创建的 ViewCell
中,我使用 SetBinding
As you can see in the TextChanged method I assign a new list to the previous one but there are no changes in the view.
In the ViewCell
that I created I assign the Text field of the Labels with the SetBinding
推荐答案
好的,这是我解决问题的方法,首先我创建了一个包装器",它为我的列表实现了 INotifyPropertyChanged
像这样作为 ItemSource :
Ok here is how I solved the problem, first of all I created a "wrapper" that implement INotifyPropertyChanged
for the list that I was taking as ItemSource like this :
public class Wrapper : INotifyPropertyChanged
{
List<Filiale> list;
JsonManager jM = new JsonManager ();//retrieve the list
public event PropertyChangedEventHandler PropertyChanged;
public NearMeViewModel ()
{
list = (jM.ReadData ()).OrderBy (x => x.distanza).ToList();//initialize the list
}
public List<Filiale> List{ //Property that will be used to get and set the item
get{ return list; }
set{
list = value;
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs("List"));// Throw!!
}
}
}
public void Reinitialize(){ // mymethod
List = (jM.ReadData ()).OrderBy (x => x.distanza).ToList();
}
然后在 NearMe 类中:
Then in the NearMe class:
Wrapper nearMeVM = new Wrapper();
public NearMe ()
{
Binding myBinding = new Binding("List");
myBinding.Source = nearMeVM;
myBinding.Path ="List";
myBinding.Mode = BindingMode.TwoWay;
listView.SetBinding (ListView.ItemsSourceProperty, myBinding);
listView.ItemTemplate = new DataTemplate(typeof(FilialeCell));
searchBar = new SearchBar {
Placeholder="Search"
};
searchBar.TextChanged += (sender, e) => {
TextChanged(searchBar.Text);
};
var stack = new StackLayout { Spacing = 0 };
stack.Children.Add (searchBar);
stack.Children.Add (listView);
Content = stack;
}
public void TextChanged(String text){
if (!String.IsNullOrEmpty (text)) {
text = text [0].ToString ().ToUpper () + text.Substring (1);
var filterSedi = nearMeVM.List.Where (filiale => filiale.nome.Contains (text));
var newList = filterSedi.ToList ();
nearMeVM.List = newList.OrderBy (x => x.distanza).ToList ();
} else {
nearMeVM.Reinitialize ();
}
这篇关于Xamarin 表单更新 listView itemSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!