2015-01-09 7 views
8

Ok ListView 개체에 List<Filiale>ItemSource으로 표시되며 개체 목록이 변경 될 때마다 ItemSource을 새로 고침하고 싶습니다. 당신의 TextChanged 방법에서 볼 수 있듯이Xamarin 양식 업데이트 listView itemSource

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; 
} 

내가 이전에 새 목록을 할당하지만보기에는 변화가 없습니다 : 목록보기 내가 이런 짓을했는지 지금은 개인화 ItemTemplate 있습니다. 내가 ObservableCollection에에 SetBinding

+0

(사용중인 경우)에서 INotifyPropertyChanged – SoftSan

+0

체크 아웃이 [링크] (http://stackoverflow.com/questions/25364513/can-i-reorder-the-listview-items에서의 ViewModel을 상속 할 수 있습니다 -in-the-run-in-xamarin-forms). –

답변

3

좋아, 내가 우선 나는이 같은 ItemSource로 촬영 된 목록 INotifyPropertyChanged 구현에 "래퍼"를 만들어 문제를 해결하는 방법입니다 :

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(); 
      } 
+0

마침내 나는 이것을 이해했다, 고마워! –

0

변경 목록으로 레이블의 텍스트 필드를 할당 및 변경을 가지고에서 INotifyPropertyChanged를 구현 만든 ViewCell에서 당신의 ListView에 반영합니다.

+0

누가 INotifyPropertyChanged를 구현해야합니까? – DQuaglio

+0

그렇게해야합니다. 항목 모델을 확장하십시오. http://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx – Martijn00

+0

그 이유가 누구였습니까? – DQuaglio

2

당신은 기본 뷰 모델을 정의하고 BaseViewModel 에서 상속됩니다 귀하의 ViewModel (예. JM)에서에서 INotifyPropertyChanged

다음
public abstract class BaseViewModel : INotifyPropertyChanged 
    { 
     protected bool ChangeAndNotify<T>(ref T property, T value, [CallerMemberName] string propertyName = "") 
     { 
      if (!EqualityComparer<T>.Default.Equals(property, value)) 
      { 
       property = value; 
       NotifyPropertyChanged(propertyName); 
       return true; 
      } 


      return false; 
     } 


     protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 

에서 상속하고 또한 ObservableCollection<YOURLISTCLASS> 목록 귀하의 필드를

을 만들 수 있습니다 ViewModel (예 : JM)은 다음과 같이 구현해야합니다.

public const string FirstNamePropertyName = "FirstName"; 
private string firstName = string.Empty; 
public string FirstName 
{ 
    get { return firstName; } 
    set { this.ChangeAndNotify(ref this.firstName, value, FirstNamePropertyName); } 
} 

희망이 있습니다. NearMe 클래스의 다음

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(); 
     } 

: 여기