How to pass List items from a window to another window in WPF?

B M-A 361 Reputation points
2023-06-04T12:47:15.6033333+00:00

Hello !

I want to pass a list as parameter from window1 to window 2 and I have this error(I do not use MVVM pattern) :

Error CS0236 A field initializer cannot reference the non-static field, method, or property

This is the code from window 1 who report the error at new Window1(person) :

  List<Person> person = new List<Person>();

        private void GenerateClick(object sender, RoutedEventArgs e)
        
		{           
            foreach (var item2 in Listview.Items)
                 {
                   person.Add((Person)item2);
                 }
             }
         Window1 win = new Window1(person);
     
        }

And this is the code for window2 :

   public Deviz(List persons)
        {
            InitializeComponent();

 var personsList = person;
		}

Best regards,

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,667 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rana Ammar 75 Reputation points
    2023-06-04T12:56:59.6233333+00:00

    In WPF (Windows Presentation Foundation), you can pass list items from one window to another by using various methods. Here are two common approaches you can take:

    1. Constructor Parameter:
    • Define a constructor in the second window that accepts a parameter of the list type.
    • When creating an instance of the second window from the first window, pass the list as a parameter to the constructor.
    • Store the passed list in a field or property within the second window for further use.

    Example:

    
    // FirstWindow.xaml.cs
    private void OpenSecondWindowButton_Click(object sender, RoutedEventArgs e)
    {
        SecondWindow secondWindow = new SecondWindow(myList);
        secondWindow.Show();
    }
    
    
    // SecondWindow.xaml.cs
    private List<string> myList;
    
    public SecondWindow(List<string> list)
    {
        InitializeComponent();
        myList = list;
    }
    
    1. Property or Field:
    • Define a public property or field in the second window to hold the list.
      • Set the property or field of the second window with the list from the first window.
      • Access the property or field in the second window to retrieve the list.
      Example:
       
       // FirstWindow.xaml.cs
       private void OpenSecondWindowButton_Click(object sender, RoutedEventArgs e)
       {
           SecondWindow secondWindow = new SecondWindow();
           secondWindow.MyList = myList;
           secondWindow.Show();
       }
    
       // SecondWindow.xaml.cs public List<string> MyList { get; set; }
    

    Remember to adjust the code based on your specific list type and window names. These methods allow you to pass the list from one window to another, making it available for use in the second window.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hui Liu-MSFT 38,026 Reputation points Microsoft Vendor
    2023-06-05T05:45:25.2466667+00:00

    Hi,@B M-A. Welcome Microsoft Q&A.

    In the updated code, the personsList field is declared in Window2 and its initialization is moved to the constructor. This way you correctly pass the list of persons from Window1 to Window2 without referencing the non-static field in the field initializer.

    Window1.xaml:

     <Grid>
            <ListView x:Name="Listview">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="120"/>
                        <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" Width="80"/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Button Click="GenerateClick" Content="generate" Width="100" Height="50" />
        </Grid>
    
    
    
    

    Window.xaml.cs:

     public partial class Window1 : Window
        {
            private List<Person> persons = new List<Person>();
            public Window1()
            {
                InitializeComponent();
                persons.Add(new Person("John Doe", 30));
                persons.Add(new Person("Jane Smith", 25));
                persons.Add(new Person("Mike Johnson", 35));
                Listview.ItemsSource = persons;
            }
            private List<Person> person = new List<Person>();
            private void GenerateClick(object sender, RoutedEventArgs e)
            {
                foreach (var item2 in Listview.Items)
                {
                    person.Add((Person)item2);
                }
    
                Window2 win = new Window2(person);
                win.Show();
            }
        }
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
    
            public Person(string name, int age)
            {
                Name = name;
                Age = age;
            }
        }
    
    
    
    

    Window2.xaml:

    <ListView x:Name="listview">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="120"/>
                        <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" Width="80"/>
                    </GridView>
                </ListView.View>
            </ListView>
    

    Window2.xaml.cs:

     public partial class Window2 : Window
        {
            private List<Person> personsList;
    
            public Window2(List<Person> persons)
            {
                InitializeComponent();
    
                personsList = persons;
               listview.ItemsSource= personsList;
            }
        }
    
    
    
    
    
    

    The result:

    4

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments