Xamarin Forms not updating ListView












0















my PropertyChanged event is not updatin my listview in xamarin.
Could someone help me with that?



The Method RefreshListView is triggered when the searchbar text has changed.



My Viewmodel:



public class LebensmittelViewModel : INotifyPropertyChanged
{
private ObservableCollection<Lebensmittel> lebensmittelList = new ObservableCollection<Lebensmittel>();
public List<Lebensmittel> normalLebensmittelList = new List<Lebensmittel>();
public event PropertyChangedEventHandler PropertyChanged;

public LebensmittelViewModel()
{
normalLebensmittelList = App.LebensmittelDatabase.getAllLebensmittel();
}

public void RefreshListView(string searchBarText)
{
LebensmittelList = addItemInCollection(searchBarText);
}

public ObservableCollection<Lebensmittel> addItemInCollection(string searchBarText)
{
if (searchBarText != null)
{
foreach (var item in normalLebensmittelList)
{
if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
{
LebensmittelList.Add(item);
};
}
}
return LebensmittelList;
}

public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public ObservableCollection<Lebensmittel> LebensmittelList
{
get
{
return lebensmittelList;
}
set
{
lebensmittelList = value;
OnPropertyChanged("LebensmittelList");
}
}
}


Einkaufsliste.xaml



<ContentPage.Content>
<StackLayout Spacing="10" Padding="10">
<SearchBar x:Name="searchBar" Text="{Binding searchBarText}" Placeholder="Lebensmittel suchen..." VerticalOptions="StartAndExpand">
<SearchBar.Behaviors>
<behavior:TextChangedBehavior/>
</SearchBar.Behaviors>
</SearchBar>
<ListView x:Name="listView" ItemsSource="{Binding LebensmittelList}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
<Label Text="{Binding Name}" YAlign="Center" Font="Large"/>
<ia:Checkbox HorizontalOptions="EndAndExpand"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>


Einkaufsliste.xaml.cs



public Einkaufsliste ()
{
InitializeComponent ();
BindingContext = new LebensmittelViewModel();
}


I think everything should be okay but it's not working.
Hope someone could help me with this,



thanks



Edit for Tom:



public void RefreshListView(string searchBarText)
{
addItemInCollection(searchBarText);
}

public void addItemInCollection(string searchBarText)
{
if (searchBarText != null)
{
foreach (var item in normalLebensmittelList)
{
if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
{
AddItemToList(item);
};
}
}
}

private void AddItemToList(Lebensmittel item)
{
lebensmittelList.Add(item);
LebensmittelList = lebensmittelList;
}









share|improve this question





























    0















    my PropertyChanged event is not updatin my listview in xamarin.
    Could someone help me with that?



    The Method RefreshListView is triggered when the searchbar text has changed.



    My Viewmodel:



    public class LebensmittelViewModel : INotifyPropertyChanged
    {
    private ObservableCollection<Lebensmittel> lebensmittelList = new ObservableCollection<Lebensmittel>();
    public List<Lebensmittel> normalLebensmittelList = new List<Lebensmittel>();
    public event PropertyChangedEventHandler PropertyChanged;

    public LebensmittelViewModel()
    {
    normalLebensmittelList = App.LebensmittelDatabase.getAllLebensmittel();
    }

    public void RefreshListView(string searchBarText)
    {
    LebensmittelList = addItemInCollection(searchBarText);
    }

    public ObservableCollection<Lebensmittel> addItemInCollection(string searchBarText)
    {
    if (searchBarText != null)
    {
    foreach (var item in normalLebensmittelList)
    {
    if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
    {
    LebensmittelList.Add(item);
    };
    }
    }
    return LebensmittelList;
    }

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public ObservableCollection<Lebensmittel> LebensmittelList
    {
    get
    {
    return lebensmittelList;
    }
    set
    {
    lebensmittelList = value;
    OnPropertyChanged("LebensmittelList");
    }
    }
    }


    Einkaufsliste.xaml



    <ContentPage.Content>
    <StackLayout Spacing="10" Padding="10">
    <SearchBar x:Name="searchBar" Text="{Binding searchBarText}" Placeholder="Lebensmittel suchen..." VerticalOptions="StartAndExpand">
    <SearchBar.Behaviors>
    <behavior:TextChangedBehavior/>
    </SearchBar.Behaviors>
    </SearchBar>
    <ListView x:Name="listView" ItemsSource="{Binding LebensmittelList}" HasUnevenRows="True">
    <ListView.ItemTemplate>
    <DataTemplate>
    <ViewCell>
    <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
    <Label Text="{Binding Name}" YAlign="Center" Font="Large"/>
    <ia:Checkbox HorizontalOptions="EndAndExpand"/>
    </StackLayout>
    </ViewCell>
    </DataTemplate>
    </ListView.ItemTemplate>
    </ListView>
    </StackLayout>
    </ContentPage.Content>


    Einkaufsliste.xaml.cs



    public Einkaufsliste ()
    {
    InitializeComponent ();
    BindingContext = new LebensmittelViewModel();
    }


    I think everything should be okay but it's not working.
    Hope someone could help me with this,



    thanks



    Edit for Tom:



    public void RefreshListView(string searchBarText)
    {
    addItemInCollection(searchBarText);
    }

    public void addItemInCollection(string searchBarText)
    {
    if (searchBarText != null)
    {
    foreach (var item in normalLebensmittelList)
    {
    if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
    {
    AddItemToList(item);
    };
    }
    }
    }

    private void AddItemToList(Lebensmittel item)
    {
    lebensmittelList.Add(item);
    LebensmittelList = lebensmittelList;
    }









    share|improve this question



























      0












      0








      0








      my PropertyChanged event is not updatin my listview in xamarin.
      Could someone help me with that?



      The Method RefreshListView is triggered when the searchbar text has changed.



      My Viewmodel:



      public class LebensmittelViewModel : INotifyPropertyChanged
      {
      private ObservableCollection<Lebensmittel> lebensmittelList = new ObservableCollection<Lebensmittel>();
      public List<Lebensmittel> normalLebensmittelList = new List<Lebensmittel>();
      public event PropertyChangedEventHandler PropertyChanged;

      public LebensmittelViewModel()
      {
      normalLebensmittelList = App.LebensmittelDatabase.getAllLebensmittel();
      }

      public void RefreshListView(string searchBarText)
      {
      LebensmittelList = addItemInCollection(searchBarText);
      }

      public ObservableCollection<Lebensmittel> addItemInCollection(string searchBarText)
      {
      if (searchBarText != null)
      {
      foreach (var item in normalLebensmittelList)
      {
      if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
      {
      LebensmittelList.Add(item);
      };
      }
      }
      return LebensmittelList;
      }

      public void OnPropertyChanged([CallerMemberName] string propertyName = null)
      {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }

      public ObservableCollection<Lebensmittel> LebensmittelList
      {
      get
      {
      return lebensmittelList;
      }
      set
      {
      lebensmittelList = value;
      OnPropertyChanged("LebensmittelList");
      }
      }
      }


      Einkaufsliste.xaml



      <ContentPage.Content>
      <StackLayout Spacing="10" Padding="10">
      <SearchBar x:Name="searchBar" Text="{Binding searchBarText}" Placeholder="Lebensmittel suchen..." VerticalOptions="StartAndExpand">
      <SearchBar.Behaviors>
      <behavior:TextChangedBehavior/>
      </SearchBar.Behaviors>
      </SearchBar>
      <ListView x:Name="listView" ItemsSource="{Binding LebensmittelList}" HasUnevenRows="True">
      <ListView.ItemTemplate>
      <DataTemplate>
      <ViewCell>
      <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
      <Label Text="{Binding Name}" YAlign="Center" Font="Large"/>
      <ia:Checkbox HorizontalOptions="EndAndExpand"/>
      </StackLayout>
      </ViewCell>
      </DataTemplate>
      </ListView.ItemTemplate>
      </ListView>
      </StackLayout>
      </ContentPage.Content>


      Einkaufsliste.xaml.cs



      public Einkaufsliste ()
      {
      InitializeComponent ();
      BindingContext = new LebensmittelViewModel();
      }


      I think everything should be okay but it's not working.
      Hope someone could help me with this,



      thanks



      Edit for Tom:



      public void RefreshListView(string searchBarText)
      {
      addItemInCollection(searchBarText);
      }

      public void addItemInCollection(string searchBarText)
      {
      if (searchBarText != null)
      {
      foreach (var item in normalLebensmittelList)
      {
      if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
      {
      AddItemToList(item);
      };
      }
      }
      }

      private void AddItemToList(Lebensmittel item)
      {
      lebensmittelList.Add(item);
      LebensmittelList = lebensmittelList;
      }









      share|improve this question
















      my PropertyChanged event is not updatin my listview in xamarin.
      Could someone help me with that?



      The Method RefreshListView is triggered when the searchbar text has changed.



      My Viewmodel:



      public class LebensmittelViewModel : INotifyPropertyChanged
      {
      private ObservableCollection<Lebensmittel> lebensmittelList = new ObservableCollection<Lebensmittel>();
      public List<Lebensmittel> normalLebensmittelList = new List<Lebensmittel>();
      public event PropertyChangedEventHandler PropertyChanged;

      public LebensmittelViewModel()
      {
      normalLebensmittelList = App.LebensmittelDatabase.getAllLebensmittel();
      }

      public void RefreshListView(string searchBarText)
      {
      LebensmittelList = addItemInCollection(searchBarText);
      }

      public ObservableCollection<Lebensmittel> addItemInCollection(string searchBarText)
      {
      if (searchBarText != null)
      {
      foreach (var item in normalLebensmittelList)
      {
      if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
      {
      LebensmittelList.Add(item);
      };
      }
      }
      return LebensmittelList;
      }

      public void OnPropertyChanged([CallerMemberName] string propertyName = null)
      {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }

      public ObservableCollection<Lebensmittel> LebensmittelList
      {
      get
      {
      return lebensmittelList;
      }
      set
      {
      lebensmittelList = value;
      OnPropertyChanged("LebensmittelList");
      }
      }
      }


      Einkaufsliste.xaml



      <ContentPage.Content>
      <StackLayout Spacing="10" Padding="10">
      <SearchBar x:Name="searchBar" Text="{Binding searchBarText}" Placeholder="Lebensmittel suchen..." VerticalOptions="StartAndExpand">
      <SearchBar.Behaviors>
      <behavior:TextChangedBehavior/>
      </SearchBar.Behaviors>
      </SearchBar>
      <ListView x:Name="listView" ItemsSource="{Binding LebensmittelList}" HasUnevenRows="True">
      <ListView.ItemTemplate>
      <DataTemplate>
      <ViewCell>
      <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
      <Label Text="{Binding Name}" YAlign="Center" Font="Large"/>
      <ia:Checkbox HorizontalOptions="EndAndExpand"/>
      </StackLayout>
      </ViewCell>
      </DataTemplate>
      </ListView.ItemTemplate>
      </ListView>
      </StackLayout>
      </ContentPage.Content>


      Einkaufsliste.xaml.cs



      public Einkaufsliste ()
      {
      InitializeComponent ();
      BindingContext = new LebensmittelViewModel();
      }


      I think everything should be okay but it's not working.
      Hope someone could help me with this,



      thanks



      Edit for Tom:



      public void RefreshListView(string searchBarText)
      {
      addItemInCollection(searchBarText);
      }

      public void addItemInCollection(string searchBarText)
      {
      if (searchBarText != null)
      {
      foreach (var item in normalLebensmittelList)
      {
      if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
      {
      AddItemToList(item);
      };
      }
      }
      }

      private void AddItemToList(Lebensmittel item)
      {
      lebensmittelList.Add(item);
      LebensmittelList = lebensmittelList;
      }






      c# listview xamarin xamarin.forms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 12:25







      Marvin L

















      asked Nov 20 '18 at 16:57









      Marvin LMarvin L

      54




      54
























          5 Answers
          5






          active

          oldest

          votes


















          0














          How it's implemented the behavior TextChangedBehavior? Do you cast the BindingContext as a LebensmittelViewModel in order to call RefreshListView?



          I would get rid of the behavior :/ and add a TextChanged event, create the property SearchBarText, since I don't see where it's declared and you're binding it to the SearchBar, refactor the RefreshListView method... and I think that it should work... :)



          Einkaufsliste.xaml.cs



          private LebensmittelViewModel vm = new LebensmittelViewModel();
          public Einkaufsliste ()
          {
          InitializeComponent ();
          BindingContext = vm;
          }

          public void OnSeachBarTextChange(object e, TextChangedEventArgs args)
          {
          vm.RefreshListView();
          }


          Einkaufsliste.xaml



          <ContentPage.Content>
          <StackLayout Spacing="10" Padding="10">
          <SearchBar x:Name="searchBar" Text="{Binding SearchBarText, Mode=TwoWay}" Placeholder="Lebensmittel suchen..." VerticalOptions="StartAndExpand" TextChanged="OnSeachBarTextChange">
          </SearchBar>
          <ListView x:Name="listView" ItemsSource="{Binding LebensmittelList}" HasUnevenRows="True">
          <ListView.ItemTemplate>
          <DataTemplate>
          <ViewCell>
          <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
          <Label Text="{Binding Name}" YAlign="Center" Font="Large"/>
          <ia:Checkbox HorizontalOptions="EndAndExpand"/>
          </StackLayout>
          </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
          </ListView>
          </StackLayout>
          </ContentPage.Content>


          Viewmodel



          private string _searchBarText;

          public string SearchBarText
          {
          get
          {
          return _searchBarText;
          }
          set
          {
          _searchBarText = value;
          OnPropertyChanged("SearchBarText");
          }
          }

          public void RefreshListView()
          {
          if (!string.IsNullOrEmpty(searchBarText))
          {
          var matches = normalLebensmittelList.Where(x => x.Name.Contains(searchBarText) || x.Name.Contains(searchBarText.First().ToString().ToUpper())
          foreach (var item in matches)
          {
          LebensmittelList.Add(item);
          }
          }
          }





          share|improve this answer


























          • omg its working, thank you so much!

            – Marvin L
            Nov 21 '18 at 12:30



















          0














          Can you try replacing the Following method:



          public void RefreshListView(string searchBarText)
          {
          LebensmittelList.Clear();
          addItemInCollection(searchBarText);
          }


          Since you are already using the Observable Collection you dont need to replace the collection. You can add and it will automatically observed and binded. Let me know if it works!






          share|improve this answer
























          • Thanks for the fast reply but this is not working :/

            – Marvin L
            Nov 20 '18 at 17:54



















          0














          It looks like you have extra work with LebensmittelList. Try do the following:



          public void RefreshListView(string searchBarText)
          {
          // this can be list clearing, if you need it
          addItemInCollection(searchBarText);
          OnPropertyChanged("LebensmittelList");
          }

          public void addItemInCollection(string searchBarText)
          {
          if (searchBarText != null)
          {
          foreach (var item in normalLebensmittelList)
          {
          if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
          {
          LebensmittelList.Add(item);
          };
          }
          }
          }


          Edit:
          I don't know if anyone can call this solution elegant, but it should work.



          internal class MyCollection<T> : ObservableCollection<T> 
          {
          public void DoCollectionChanged()
          {
          OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
          }

          }

          ... MyCollection<LebensmittelItem> LebensmittelList; // field and property declaration should be changed

          public void RefreshListView(string searchBarText)
          {
          // this can be list clearing, if you need it
          addItemInCollection(searchBarText);
          LebensmittelList.DoCollectionChanged();
          }





          share|improve this answer


























          • thanks but not working :/

            – Marvin L
            Nov 20 '18 at 19:58











          • I updated the answer, please try this.

            – Miamy
            Nov 20 '18 at 20:25











          • Not working :/ but thanks anyway

            – Marvin L
            Nov 20 '18 at 21:42



















          0














          Is OnPropertyChanged in the property set called when you do the RefreshListView?






          share|improve this answer
























          • Yes it its in and it jumps also into this but nothing is working

            – Marvin L
            Nov 21 '18 at 10:43



















          0














          In the addItemToCollection method, you are calling the following line:



          LebensmittelList.Add(item);


          In this case LebensmittelList is a property, not a variable. The line LebensmittelList.Add(item); is effectively doing the following:



          var temporaryList = LebensmittelList; // Gets lebensmittelList and assigns to temporaryList
          temporaryList.Add(item); // You add the item to the *variable temporaryList*


          Essentially, you are adding to an instance of lebensmittelList (a temporary copy), and not the lebensmittelList variable. Once you have added that item to the temporary list, the whole (temporary) list gets discarded without "saving changes".



          To actually update LebensmittelList, you would be better off calling a method like:



          private void AddItemToList(object item)
          {
          lebensmittelList.Add(item);
          LebensmittelList = lebensmittelList;
          }





          share|improve this answer
























          • Unfortunately does not work :/ I can upload my whole project somewhere

            – Marvin L
            Nov 21 '18 at 11:47











          • Can you post what you have tried?

            – Tom
            Nov 21 '18 at 12:14











          • I added it for you in my original post

            – Marvin L
            Nov 21 '18 at 12:25











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397884%2fxamarin-forms-not-updating-listview%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          How it's implemented the behavior TextChangedBehavior? Do you cast the BindingContext as a LebensmittelViewModel in order to call RefreshListView?



          I would get rid of the behavior :/ and add a TextChanged event, create the property SearchBarText, since I don't see where it's declared and you're binding it to the SearchBar, refactor the RefreshListView method... and I think that it should work... :)



          Einkaufsliste.xaml.cs



          private LebensmittelViewModel vm = new LebensmittelViewModel();
          public Einkaufsliste ()
          {
          InitializeComponent ();
          BindingContext = vm;
          }

          public void OnSeachBarTextChange(object e, TextChangedEventArgs args)
          {
          vm.RefreshListView();
          }


          Einkaufsliste.xaml



          <ContentPage.Content>
          <StackLayout Spacing="10" Padding="10">
          <SearchBar x:Name="searchBar" Text="{Binding SearchBarText, Mode=TwoWay}" Placeholder="Lebensmittel suchen..." VerticalOptions="StartAndExpand" TextChanged="OnSeachBarTextChange">
          </SearchBar>
          <ListView x:Name="listView" ItemsSource="{Binding LebensmittelList}" HasUnevenRows="True">
          <ListView.ItemTemplate>
          <DataTemplate>
          <ViewCell>
          <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
          <Label Text="{Binding Name}" YAlign="Center" Font="Large"/>
          <ia:Checkbox HorizontalOptions="EndAndExpand"/>
          </StackLayout>
          </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
          </ListView>
          </StackLayout>
          </ContentPage.Content>


          Viewmodel



          private string _searchBarText;

          public string SearchBarText
          {
          get
          {
          return _searchBarText;
          }
          set
          {
          _searchBarText = value;
          OnPropertyChanged("SearchBarText");
          }
          }

          public void RefreshListView()
          {
          if (!string.IsNullOrEmpty(searchBarText))
          {
          var matches = normalLebensmittelList.Where(x => x.Name.Contains(searchBarText) || x.Name.Contains(searchBarText.First().ToString().ToUpper())
          foreach (var item in matches)
          {
          LebensmittelList.Add(item);
          }
          }
          }





          share|improve this answer


























          • omg its working, thank you so much!

            – Marvin L
            Nov 21 '18 at 12:30
















          0














          How it's implemented the behavior TextChangedBehavior? Do you cast the BindingContext as a LebensmittelViewModel in order to call RefreshListView?



          I would get rid of the behavior :/ and add a TextChanged event, create the property SearchBarText, since I don't see where it's declared and you're binding it to the SearchBar, refactor the RefreshListView method... and I think that it should work... :)



          Einkaufsliste.xaml.cs



          private LebensmittelViewModel vm = new LebensmittelViewModel();
          public Einkaufsliste ()
          {
          InitializeComponent ();
          BindingContext = vm;
          }

          public void OnSeachBarTextChange(object e, TextChangedEventArgs args)
          {
          vm.RefreshListView();
          }


          Einkaufsliste.xaml



          <ContentPage.Content>
          <StackLayout Spacing="10" Padding="10">
          <SearchBar x:Name="searchBar" Text="{Binding SearchBarText, Mode=TwoWay}" Placeholder="Lebensmittel suchen..." VerticalOptions="StartAndExpand" TextChanged="OnSeachBarTextChange">
          </SearchBar>
          <ListView x:Name="listView" ItemsSource="{Binding LebensmittelList}" HasUnevenRows="True">
          <ListView.ItemTemplate>
          <DataTemplate>
          <ViewCell>
          <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
          <Label Text="{Binding Name}" YAlign="Center" Font="Large"/>
          <ia:Checkbox HorizontalOptions="EndAndExpand"/>
          </StackLayout>
          </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
          </ListView>
          </StackLayout>
          </ContentPage.Content>


          Viewmodel



          private string _searchBarText;

          public string SearchBarText
          {
          get
          {
          return _searchBarText;
          }
          set
          {
          _searchBarText = value;
          OnPropertyChanged("SearchBarText");
          }
          }

          public void RefreshListView()
          {
          if (!string.IsNullOrEmpty(searchBarText))
          {
          var matches = normalLebensmittelList.Where(x => x.Name.Contains(searchBarText) || x.Name.Contains(searchBarText.First().ToString().ToUpper())
          foreach (var item in matches)
          {
          LebensmittelList.Add(item);
          }
          }
          }





          share|improve this answer


























          • omg its working, thank you so much!

            – Marvin L
            Nov 21 '18 at 12:30














          0












          0








          0







          How it's implemented the behavior TextChangedBehavior? Do you cast the BindingContext as a LebensmittelViewModel in order to call RefreshListView?



          I would get rid of the behavior :/ and add a TextChanged event, create the property SearchBarText, since I don't see where it's declared and you're binding it to the SearchBar, refactor the RefreshListView method... and I think that it should work... :)



          Einkaufsliste.xaml.cs



          private LebensmittelViewModel vm = new LebensmittelViewModel();
          public Einkaufsliste ()
          {
          InitializeComponent ();
          BindingContext = vm;
          }

          public void OnSeachBarTextChange(object e, TextChangedEventArgs args)
          {
          vm.RefreshListView();
          }


          Einkaufsliste.xaml



          <ContentPage.Content>
          <StackLayout Spacing="10" Padding="10">
          <SearchBar x:Name="searchBar" Text="{Binding SearchBarText, Mode=TwoWay}" Placeholder="Lebensmittel suchen..." VerticalOptions="StartAndExpand" TextChanged="OnSeachBarTextChange">
          </SearchBar>
          <ListView x:Name="listView" ItemsSource="{Binding LebensmittelList}" HasUnevenRows="True">
          <ListView.ItemTemplate>
          <DataTemplate>
          <ViewCell>
          <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
          <Label Text="{Binding Name}" YAlign="Center" Font="Large"/>
          <ia:Checkbox HorizontalOptions="EndAndExpand"/>
          </StackLayout>
          </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
          </ListView>
          </StackLayout>
          </ContentPage.Content>


          Viewmodel



          private string _searchBarText;

          public string SearchBarText
          {
          get
          {
          return _searchBarText;
          }
          set
          {
          _searchBarText = value;
          OnPropertyChanged("SearchBarText");
          }
          }

          public void RefreshListView()
          {
          if (!string.IsNullOrEmpty(searchBarText))
          {
          var matches = normalLebensmittelList.Where(x => x.Name.Contains(searchBarText) || x.Name.Contains(searchBarText.First().ToString().ToUpper())
          foreach (var item in matches)
          {
          LebensmittelList.Add(item);
          }
          }
          }





          share|improve this answer















          How it's implemented the behavior TextChangedBehavior? Do you cast the BindingContext as a LebensmittelViewModel in order to call RefreshListView?



          I would get rid of the behavior :/ and add a TextChanged event, create the property SearchBarText, since I don't see where it's declared and you're binding it to the SearchBar, refactor the RefreshListView method... and I think that it should work... :)



          Einkaufsliste.xaml.cs



          private LebensmittelViewModel vm = new LebensmittelViewModel();
          public Einkaufsliste ()
          {
          InitializeComponent ();
          BindingContext = vm;
          }

          public void OnSeachBarTextChange(object e, TextChangedEventArgs args)
          {
          vm.RefreshListView();
          }


          Einkaufsliste.xaml



          <ContentPage.Content>
          <StackLayout Spacing="10" Padding="10">
          <SearchBar x:Name="searchBar" Text="{Binding SearchBarText, Mode=TwoWay}" Placeholder="Lebensmittel suchen..." VerticalOptions="StartAndExpand" TextChanged="OnSeachBarTextChange">
          </SearchBar>
          <ListView x:Name="listView" ItemsSource="{Binding LebensmittelList}" HasUnevenRows="True">
          <ListView.ItemTemplate>
          <DataTemplate>
          <ViewCell>
          <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
          <Label Text="{Binding Name}" YAlign="Center" Font="Large"/>
          <ia:Checkbox HorizontalOptions="EndAndExpand"/>
          </StackLayout>
          </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
          </ListView>
          </StackLayout>
          </ContentPage.Content>


          Viewmodel



          private string _searchBarText;

          public string SearchBarText
          {
          get
          {
          return _searchBarText;
          }
          set
          {
          _searchBarText = value;
          OnPropertyChanged("SearchBarText");
          }
          }

          public void RefreshListView()
          {
          if (!string.IsNullOrEmpty(searchBarText))
          {
          var matches = normalLebensmittelList.Where(x => x.Name.Contains(searchBarText) || x.Name.Contains(searchBarText.First().ToString().ToUpper())
          foreach (var item in matches)
          {
          LebensmittelList.Add(item);
          }
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 12:01

























          answered Nov 21 '18 at 11:55









          jjchiwjjchiw

          3,2952427




          3,2952427













          • omg its working, thank you so much!

            – Marvin L
            Nov 21 '18 at 12:30



















          • omg its working, thank you so much!

            – Marvin L
            Nov 21 '18 at 12:30

















          omg its working, thank you so much!

          – Marvin L
          Nov 21 '18 at 12:30





          omg its working, thank you so much!

          – Marvin L
          Nov 21 '18 at 12:30













          0














          Can you try replacing the Following method:



          public void RefreshListView(string searchBarText)
          {
          LebensmittelList.Clear();
          addItemInCollection(searchBarText);
          }


          Since you are already using the Observable Collection you dont need to replace the collection. You can add and it will automatically observed and binded. Let me know if it works!






          share|improve this answer
























          • Thanks for the fast reply but this is not working :/

            – Marvin L
            Nov 20 '18 at 17:54
















          0














          Can you try replacing the Following method:



          public void RefreshListView(string searchBarText)
          {
          LebensmittelList.Clear();
          addItemInCollection(searchBarText);
          }


          Since you are already using the Observable Collection you dont need to replace the collection. You can add and it will automatically observed and binded. Let me know if it works!






          share|improve this answer
























          • Thanks for the fast reply but this is not working :/

            – Marvin L
            Nov 20 '18 at 17:54














          0












          0








          0







          Can you try replacing the Following method:



          public void RefreshListView(string searchBarText)
          {
          LebensmittelList.Clear();
          addItemInCollection(searchBarText);
          }


          Since you are already using the Observable Collection you dont need to replace the collection. You can add and it will automatically observed and binded. Let me know if it works!






          share|improve this answer













          Can you try replacing the Following method:



          public void RefreshListView(string searchBarText)
          {
          LebensmittelList.Clear();
          addItemInCollection(searchBarText);
          }


          Since you are already using the Observable Collection you dont need to replace the collection. You can add and it will automatically observed and binded. Let me know if it works!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 17:44









          Nirmal SubediNirmal Subedi

          1,4382923




          1,4382923













          • Thanks for the fast reply but this is not working :/

            – Marvin L
            Nov 20 '18 at 17:54



















          • Thanks for the fast reply but this is not working :/

            – Marvin L
            Nov 20 '18 at 17:54

















          Thanks for the fast reply but this is not working :/

          – Marvin L
          Nov 20 '18 at 17:54





          Thanks for the fast reply but this is not working :/

          – Marvin L
          Nov 20 '18 at 17:54











          0














          It looks like you have extra work with LebensmittelList. Try do the following:



          public void RefreshListView(string searchBarText)
          {
          // this can be list clearing, if you need it
          addItemInCollection(searchBarText);
          OnPropertyChanged("LebensmittelList");
          }

          public void addItemInCollection(string searchBarText)
          {
          if (searchBarText != null)
          {
          foreach (var item in normalLebensmittelList)
          {
          if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
          {
          LebensmittelList.Add(item);
          };
          }
          }
          }


          Edit:
          I don't know if anyone can call this solution elegant, but it should work.



          internal class MyCollection<T> : ObservableCollection<T> 
          {
          public void DoCollectionChanged()
          {
          OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
          }

          }

          ... MyCollection<LebensmittelItem> LebensmittelList; // field and property declaration should be changed

          public void RefreshListView(string searchBarText)
          {
          // this can be list clearing, if you need it
          addItemInCollection(searchBarText);
          LebensmittelList.DoCollectionChanged();
          }





          share|improve this answer


























          • thanks but not working :/

            – Marvin L
            Nov 20 '18 at 19:58











          • I updated the answer, please try this.

            – Miamy
            Nov 20 '18 at 20:25











          • Not working :/ but thanks anyway

            – Marvin L
            Nov 20 '18 at 21:42
















          0














          It looks like you have extra work with LebensmittelList. Try do the following:



          public void RefreshListView(string searchBarText)
          {
          // this can be list clearing, if you need it
          addItemInCollection(searchBarText);
          OnPropertyChanged("LebensmittelList");
          }

          public void addItemInCollection(string searchBarText)
          {
          if (searchBarText != null)
          {
          foreach (var item in normalLebensmittelList)
          {
          if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
          {
          LebensmittelList.Add(item);
          };
          }
          }
          }


          Edit:
          I don't know if anyone can call this solution elegant, but it should work.



          internal class MyCollection<T> : ObservableCollection<T> 
          {
          public void DoCollectionChanged()
          {
          OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
          }

          }

          ... MyCollection<LebensmittelItem> LebensmittelList; // field and property declaration should be changed

          public void RefreshListView(string searchBarText)
          {
          // this can be list clearing, if you need it
          addItemInCollection(searchBarText);
          LebensmittelList.DoCollectionChanged();
          }





          share|improve this answer


























          • thanks but not working :/

            – Marvin L
            Nov 20 '18 at 19:58











          • I updated the answer, please try this.

            – Miamy
            Nov 20 '18 at 20:25











          • Not working :/ but thanks anyway

            – Marvin L
            Nov 20 '18 at 21:42














          0












          0








          0







          It looks like you have extra work with LebensmittelList. Try do the following:



          public void RefreshListView(string searchBarText)
          {
          // this can be list clearing, if you need it
          addItemInCollection(searchBarText);
          OnPropertyChanged("LebensmittelList");
          }

          public void addItemInCollection(string searchBarText)
          {
          if (searchBarText != null)
          {
          foreach (var item in normalLebensmittelList)
          {
          if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
          {
          LebensmittelList.Add(item);
          };
          }
          }
          }


          Edit:
          I don't know if anyone can call this solution elegant, but it should work.



          internal class MyCollection<T> : ObservableCollection<T> 
          {
          public void DoCollectionChanged()
          {
          OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
          }

          }

          ... MyCollection<LebensmittelItem> LebensmittelList; // field and property declaration should be changed

          public void RefreshListView(string searchBarText)
          {
          // this can be list clearing, if you need it
          addItemInCollection(searchBarText);
          LebensmittelList.DoCollectionChanged();
          }





          share|improve this answer















          It looks like you have extra work with LebensmittelList. Try do the following:



          public void RefreshListView(string searchBarText)
          {
          // this can be list clearing, if you need it
          addItemInCollection(searchBarText);
          OnPropertyChanged("LebensmittelList");
          }

          public void addItemInCollection(string searchBarText)
          {
          if (searchBarText != null)
          {
          foreach (var item in normalLebensmittelList)
          {
          if (item.Name.Contains(searchBarText) || item.Name.Contains(searchBarText.First().ToString().ToUpper()))
          {
          LebensmittelList.Add(item);
          };
          }
          }
          }


          Edit:
          I don't know if anyone can call this solution elegant, but it should work.



          internal class MyCollection<T> : ObservableCollection<T> 
          {
          public void DoCollectionChanged()
          {
          OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
          }

          }

          ... MyCollection<LebensmittelItem> LebensmittelList; // field and property declaration should be changed

          public void RefreshListView(string searchBarText)
          {
          // this can be list clearing, if you need it
          addItemInCollection(searchBarText);
          LebensmittelList.DoCollectionChanged();
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 20:24

























          answered Nov 20 '18 at 19:43









          MiamyMiamy

          1,083715




          1,083715













          • thanks but not working :/

            – Marvin L
            Nov 20 '18 at 19:58











          • I updated the answer, please try this.

            – Miamy
            Nov 20 '18 at 20:25











          • Not working :/ but thanks anyway

            – Marvin L
            Nov 20 '18 at 21:42



















          • thanks but not working :/

            – Marvin L
            Nov 20 '18 at 19:58











          • I updated the answer, please try this.

            – Miamy
            Nov 20 '18 at 20:25











          • Not working :/ but thanks anyway

            – Marvin L
            Nov 20 '18 at 21:42

















          thanks but not working :/

          – Marvin L
          Nov 20 '18 at 19:58





          thanks but not working :/

          – Marvin L
          Nov 20 '18 at 19:58













          I updated the answer, please try this.

          – Miamy
          Nov 20 '18 at 20:25





          I updated the answer, please try this.

          – Miamy
          Nov 20 '18 at 20:25













          Not working :/ but thanks anyway

          – Marvin L
          Nov 20 '18 at 21:42





          Not working :/ but thanks anyway

          – Marvin L
          Nov 20 '18 at 21:42











          0














          Is OnPropertyChanged in the property set called when you do the RefreshListView?






          share|improve this answer
























          • Yes it its in and it jumps also into this but nothing is working

            – Marvin L
            Nov 21 '18 at 10:43
















          0














          Is OnPropertyChanged in the property set called when you do the RefreshListView?






          share|improve this answer
























          • Yes it its in and it jumps also into this but nothing is working

            – Marvin L
            Nov 21 '18 at 10:43














          0












          0








          0







          Is OnPropertyChanged in the property set called when you do the RefreshListView?






          share|improve this answer













          Is OnPropertyChanged in the property set called when you do the RefreshListView?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 9:27









          Alexander UddfeldtAlexander Uddfeldt

          2066




          2066













          • Yes it its in and it jumps also into this but nothing is working

            – Marvin L
            Nov 21 '18 at 10:43



















          • Yes it its in and it jumps also into this but nothing is working

            – Marvin L
            Nov 21 '18 at 10:43

















          Yes it its in and it jumps also into this but nothing is working

          – Marvin L
          Nov 21 '18 at 10:43





          Yes it its in and it jumps also into this but nothing is working

          – Marvin L
          Nov 21 '18 at 10:43











          0














          In the addItemToCollection method, you are calling the following line:



          LebensmittelList.Add(item);


          In this case LebensmittelList is a property, not a variable. The line LebensmittelList.Add(item); is effectively doing the following:



          var temporaryList = LebensmittelList; // Gets lebensmittelList and assigns to temporaryList
          temporaryList.Add(item); // You add the item to the *variable temporaryList*


          Essentially, you are adding to an instance of lebensmittelList (a temporary copy), and not the lebensmittelList variable. Once you have added that item to the temporary list, the whole (temporary) list gets discarded without "saving changes".



          To actually update LebensmittelList, you would be better off calling a method like:



          private void AddItemToList(object item)
          {
          lebensmittelList.Add(item);
          LebensmittelList = lebensmittelList;
          }





          share|improve this answer
























          • Unfortunately does not work :/ I can upload my whole project somewhere

            – Marvin L
            Nov 21 '18 at 11:47











          • Can you post what you have tried?

            – Tom
            Nov 21 '18 at 12:14











          • I added it for you in my original post

            – Marvin L
            Nov 21 '18 at 12:25
















          0














          In the addItemToCollection method, you are calling the following line:



          LebensmittelList.Add(item);


          In this case LebensmittelList is a property, not a variable. The line LebensmittelList.Add(item); is effectively doing the following:



          var temporaryList = LebensmittelList; // Gets lebensmittelList and assigns to temporaryList
          temporaryList.Add(item); // You add the item to the *variable temporaryList*


          Essentially, you are adding to an instance of lebensmittelList (a temporary copy), and not the lebensmittelList variable. Once you have added that item to the temporary list, the whole (temporary) list gets discarded without "saving changes".



          To actually update LebensmittelList, you would be better off calling a method like:



          private void AddItemToList(object item)
          {
          lebensmittelList.Add(item);
          LebensmittelList = lebensmittelList;
          }





          share|improve this answer
























          • Unfortunately does not work :/ I can upload my whole project somewhere

            – Marvin L
            Nov 21 '18 at 11:47











          • Can you post what you have tried?

            – Tom
            Nov 21 '18 at 12:14











          • I added it for you in my original post

            – Marvin L
            Nov 21 '18 at 12:25














          0












          0








          0







          In the addItemToCollection method, you are calling the following line:



          LebensmittelList.Add(item);


          In this case LebensmittelList is a property, not a variable. The line LebensmittelList.Add(item); is effectively doing the following:



          var temporaryList = LebensmittelList; // Gets lebensmittelList and assigns to temporaryList
          temporaryList.Add(item); // You add the item to the *variable temporaryList*


          Essentially, you are adding to an instance of lebensmittelList (a temporary copy), and not the lebensmittelList variable. Once you have added that item to the temporary list, the whole (temporary) list gets discarded without "saving changes".



          To actually update LebensmittelList, you would be better off calling a method like:



          private void AddItemToList(object item)
          {
          lebensmittelList.Add(item);
          LebensmittelList = lebensmittelList;
          }





          share|improve this answer













          In the addItemToCollection method, you are calling the following line:



          LebensmittelList.Add(item);


          In this case LebensmittelList is a property, not a variable. The line LebensmittelList.Add(item); is effectively doing the following:



          var temporaryList = LebensmittelList; // Gets lebensmittelList and assigns to temporaryList
          temporaryList.Add(item); // You add the item to the *variable temporaryList*


          Essentially, you are adding to an instance of lebensmittelList (a temporary copy), and not the lebensmittelList variable. Once you have added that item to the temporary list, the whole (temporary) list gets discarded without "saving changes".



          To actually update LebensmittelList, you would be better off calling a method like:



          private void AddItemToList(object item)
          {
          lebensmittelList.Add(item);
          LebensmittelList = lebensmittelList;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 11:04









          TomTom

          1,112717




          1,112717













          • Unfortunately does not work :/ I can upload my whole project somewhere

            – Marvin L
            Nov 21 '18 at 11:47











          • Can you post what you have tried?

            – Tom
            Nov 21 '18 at 12:14











          • I added it for you in my original post

            – Marvin L
            Nov 21 '18 at 12:25



















          • Unfortunately does not work :/ I can upload my whole project somewhere

            – Marvin L
            Nov 21 '18 at 11:47











          • Can you post what you have tried?

            – Tom
            Nov 21 '18 at 12:14











          • I added it for you in my original post

            – Marvin L
            Nov 21 '18 at 12:25

















          Unfortunately does not work :/ I can upload my whole project somewhere

          – Marvin L
          Nov 21 '18 at 11:47





          Unfortunately does not work :/ I can upload my whole project somewhere

          – Marvin L
          Nov 21 '18 at 11:47













          Can you post what you have tried?

          – Tom
          Nov 21 '18 at 12:14





          Can you post what you have tried?

          – Tom
          Nov 21 '18 at 12:14













          I added it for you in my original post

          – Marvin L
          Nov 21 '18 at 12:25





          I added it for you in my original post

          – Marvin L
          Nov 21 '18 at 12:25


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397884%2fxamarin-forms-not-updating-listview%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          Npm cannot find a required file even through it is in the searched directory

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith