locked
Foreach loop on listview items? RRS feed

  • Question

  • Hi, so i have declared my collection in my class like:

    ObservableCollection<User> items = new ObservableCollection<User>();
    

    i then have a method:

            public class User
            {
                public string First { get; set; }
                public string Last { get; set; }
                public string Nick { get; set; }
            }

    An then im adding to my listview like this:

                InitializeComponent();
                items.Add(new User() { First = firstName, Last = LastName, Nick = nick});
                playerlist2.ItemsSource = items;

    This all works great,

    Now im trying to do a foreach loop...

    I want to loop through each item that ive added:

    Iv'e been trying it like this: 

               foreach (var item in playerlist2.ItemsSource)
                    { }
    Yet when it runs, it only seems to loop through my first in in the list?

    I'm fairly new to code so im guessing this is some stupid mistake on my part, can anyone please advise?

    Thank you.  

    Wednesday, October 29, 2014 5:55 PM

Answers

  • If I have a listbox and a button on a window.

    In code behind I do:

            public MainWindow()
            {
                InitializeComponent();
                items.Add("One");
                lb.ItemsSource = items;
            }
            private ObservableCollection<string> items = new ObservableCollection<string>();
    
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                items.Add("two");
                foreach (string  itm in items)
                {
                    Debug.WriteLine(itm);
                }
            }

    I see One and two in the listbox and I get

    one

    two

    Output, as expected.

    Wednesday, October 29, 2014 8:45 PM

All replies

  • Rather than adding items, you're better off using binding.

    You can then

    foreach(User user in items)
    {
    }

    I think maybe the simplest sample I have which uses that approach is this:

    https://gallery.technet.microsoft.com/WPF-Dialler-simulator-d782db17

    I set the datacontext of MainWindow to an instance of MainWindowViewmodel

    That has a couple of observablecollections the view binds to:

    Phonelines and Calls.

        public class MainWindowViewModel : NotifyUIBase
        {
            public ObservableCollection<PhoneLine> PhoneLines { get; set; }
            public ObservableCollection<Call> Calls { get; set; }

    and they are bound:

            <ItemsControl ItemsSource="{Binding PhoneLines}" Grid.Row="1">
                <ItemsControl.ItemTemplate>

    and

            <ListBox ItemsSource="{Binding Calls}" Grid.Column="1" Grid.Row="1" Grid.RowSpan="2">
            <ListBox.ItemTemplate>
                <DataTemplate>
    I notice your code only adds one item there.

    Wednesday, October 29, 2014 6:26 PM
  • yes i noticed id done it a little wrong. 

    From what i can tell my code now adds to the collection properly. 

    On the click of an "Add" button, i am.... 

                InitializeComponent();
                User items1 = new User { Rating = rating, First = firstName, Last = LastName, Nick = nick};
                items.Add(items1);
                playerlist2.ItemsSource = items;

    Ok... great, they all show up in my listview.

    Im also able to remove them with the click of a remove button and: 

    items.Remove((User)this.playerlist2.SelectedItem);

    So... im pretty sure there being add to the collection, and im able to see them on my list....

    But when i try:

            foreach (User user in items)
                    { 

    Im still only getting a loop through the first item.... 

    If each time i click to add, they populate my listview? So they must be in the collection shouldn't they? So confused... 



    • Edited by CraigBaines Wednesday, October 29, 2014 7:23 PM
    Wednesday, October 29, 2014 6:52 PM
  • If I have a listbox and a button on a window.

    In code behind I do:

            public MainWindow()
            {
                InitializeComponent();
                items.Add("One");
                lb.ItemsSource = items;
            }
            private ObservableCollection<string> items = new ObservableCollection<string>();
    
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                items.Add("two");
                foreach (string  itm in items)
                {
                    Debug.WriteLine(itm);
                }
            }

    I see One and two in the listbox and I get

    one

    two

    Output, as expected.

    Wednesday, October 29, 2014 8:45 PM
  • resolved Ty. 

    • Edited by CraigBaines Thursday, October 30, 2014 7:21 AM
    Wednesday, October 29, 2014 9:44 PM