Dav*_*lio 9 .net c# xamarin xamarin.forms
好吧,我有一个ListView
具有List<Filiale>
as 的对象,ItemSource
我想ItemSource
在对象列表更改时刷新.ListView有一个个性化的ItemTemplate
现在我已经这样做了:
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;
}
Run Code Online (Sandbox Code Playgroud)
正如您在TextChanged方法中看到的,我为前一个方法分配了一个新列表,但视图中没有任何更改.在ViewCell
我创建的内容中,我使用了分配标签的文本字段SetBinding
dan*_*ham 15
您可以将ListView的ItemsSource设置为null,然后再将其重新设置为表重新加载. http://forums.xamarin.com/discussion/18868/tableview-reloaddata-equivalent-for-listview
好的,这是我解决问题的方法,首先,我创建了一个“包装器”,该包装器INotifyPropertyChanged
为像这样作为ItemSource的列表实现了:
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();
}
Run Code Online (Sandbox Code Playgroud)
然后在NearMe类中:
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 ();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
33987 次 |
最近记录: |