好的,我有一个ListView对象,它的List<Filiale>作为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;
}正如您在TextChanged方法中所看到的,我为前一个列表分配了一个新列表,但是视图中没有任何更改。在我创建的ViewCell中,我用SetBinding分配标签的文本字段
发布于 2015-01-12 08:47:55
下面是我如何解决这个问题的方法,首先,我创建了一个“包装器”,它为我作为ItemSource使用的列表实现了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();
}然后在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 ();
}发布于 2015-03-31 21:54:13
您可以将ItemsSource的ListView设置为null,然后再次将其设置为空,这样就可以重新加载表。http://forums.xamarin.com/discussion/18868/tableview-reloaddata-equivalent-for-listview
发布于 2015-01-09 10:24:47
将列表更改为ObservableCollection,并实现INotifyPropertyChanged以使更改反映在ListView中。
https://stackoverflow.com/questions/27857975
复制相似问题