2016-05-14 2 views
0

Ich schaffe beobachtbaren Sammlung in der KlasseWarum ändern sich Daten in bindendem XAML nicht?

public ObservableCollection<NewsTags> NewsTagsList { get; private set; } 

public RelayCommand RefreshNewsTags 
{ 
    get 
    { 
     return _refreshNewsTagsCommand ?? (
      _refreshNewsTagsCommand = new RelayCommand(
       async() => { 
        var list = await _newsTagsService.GetAllAsync(1, false); 
        foreach (var item in list) 
        { 
         NewsTagsList.Add(item); 
        } 
       } 
     )); 
    } 
} 

und Anrufsteuerung individuelle

<tagsListSelector:BoxSelector Items="{Binding NewsTagsList}" /> 

Und boxselector.cs

public partial class BoxSelector : ContentView 
{ 
    public BoxSelector() 
    { 
     InitializeComponent(); 
    } 

    public static readonly BindableProperty ItemsProperty = 
     BindableProperty.Create<BoxSelector, IEnumerable>(
      view => view.ItemsSource, 
      null, 
      propertyChanged: (bindableObject, oldValue, newValue) => { 

       ((BoxSelector)bindableObject).ItemsSourceChanged(bindableObject, oldValue, newValue); 
      } 
     ); 

    private void ItemsSourceChanged(BindableObject bindableObject, IEnumerable oldvalue, IEnumerable newvalue) 
    { 
     boxSelectorGrid.Children.Clear(); 

     foreach (object obj in newvalue) 
     { 
      boxSelectorGrid.Children.Add(new Label { Text="LOL", TextColor= Color.FromHex("#000")}); 
     } 
    } 

aber keine Änderungen. ps.s in newslist.cs NewsTagsList.Count == 10 aber in BoxSelector == 0

was mache ich falsch?

Antwort

1

Wenn in der ItemsSourceChanged-Methode der newValue vom Typ INotifyCollectionChanged ist, müssen Sie das CollectionChanged-Ereignis verdrahten und innerhalb des CollectionChanged-Codes alles tun, was Sie tun möchten, wenn sich die Auflistung ändert. Sie möchten sich auch vom Ereignis auf dem alten Wert abmelden, wenn es vom Typ INotifyCollectionChanged ist.

Etwas wie folgt aus:

 private void ItemsSourceChanged(BindableObject bindableObject, IEnumerable oldvalue, IEnumerable newvalue) 
    { 
     var incc = oldvalue as INotifyCollectionChanged; 
     if (incc != null) 
     { 
      incc.CollectionChanged -= Incc_CollectionChanged; 
     } 
     incc = newvalue as INotifyCollectionChanged; 
     if (incc != null) 
     { 
      incc.CollectionChanged += Incc_CollectionChanged; 
     } 
     boxSelectorGrid.Children.Clear(); 

     foreach (object obj in newvalue) 
     { 
      boxSelectorGrid.Children.Add(new Label { Text = "LOL", TextColor = Color.FromHex("#000") }); 
     } 
    } 

    private void Incc_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    { 
     switch (e.Action) 
     { 
      case NotifyCollectionChangedAction.Add: 
       // handle Added rows 
       break; 
      case NotifyCollectionChangedAction.Remove: 
       // handle deleting rows 
       break; 
      case NotifyCollectionChangedAction.Move: 
       // handle moving rows 
       break; 
      case NotifyCollectionChangedAction.Replace: 
       // handle replacing rows 
       break; 
      case NotifyCollectionChangedAction.Reset: 
       // reload everything 
       break; 
     } 
    } 
+0

kann ein Beispiel sein, bitte – bleggleb