Xamarin形式更新的ListView的ItemSource [英] Xamarin form update listView itemSource
问题描述
好吧,我有其中有一个的ListView
对象的列表<分公司>
为的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的<一个包装 / code>对于我正在为的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屋!