2015-01-09 6 views
8

ठीक है, मैं एक ListView वस्तु है जो एक List<Filiale>ItemSource के रूप में है और मैं ItemSource ताज़ा करना चाहते जब भी वस्तु परिवर्तन की सूची। ListView एक व्यक्तिगत ItemTemplate अभी के लिए मैंने यह किया है है: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; 
} 

मैं पिछले एक के लिए एक नई सूची आवंटित आप TextChanged विधि में देख सकते हैं, लेकिन ध्यान में रखते हुए कोई बदलाव नहीं देखते हैं। ViewCell मैं बनाया है कि मैं SetBinding

+0

आप (यदि आप उपयोग कर रहे हैं) INotifyPropertyChanged – SoftSan

+0

चेक बाहर इस [कड़ी] (http://stackoverflow.com/questions/25364513/can-i-reorder-the-listview-items से अपने viewmodel वारिस कर सकते हैं -ऑन-द-रन-इन-एक्समारिन-फॉर्म) जो आपकी मदद कर सकते हैं .. –

उत्तर

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

ObservableCollection सूची बदलें साथ लेबल का पाठ क्षेत्र आवंटित और INotifyPropertyChanged लागू परिवर्तन करने के लिए अपने ListView में दर्शाते हैं।

+0

INotifyProperty को किसने कार्यान्वित किया जाना चाहिए? – DQuaglio

+0

आपको यह करना चाहिए। इसके साथ आइटम मॉडल बढ़ाएं: http://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx – Martijn00

+0

यह क्यों नहीं था – DQuaglio

2

आप एक आधार दृश्य मॉडल को परिभाषित करने और अपने viewmodel (उदा। जेएम) में 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; 
    } 

तब से यह वारिस BaseViewModel से विरासत की जाएगी और ObservableCollection<YOURLISTCLASS> सूची

इसके अलावा अपने क्षेत्रों बना सकते हैं कर सकते हैं व्यूमोडेल (एक्स। जेएम) में निम्नलिखित की तरह लागू होना चाहिए:

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

आशा है कि इससे मदद मिलती है।

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 कक्षा में

: