简体   繁体   中英

C# WPF adding object to collection in a new window

So I'm starting my adventure with WPF and I wanted to make a simple application that has 2 windows. One would have a button that triggers a new window to come up in which there would be an option of adding a new object to my ObservableColletion . And I managed to make the two windows, but after creating the new window the new .cs file doesn't see the collection that was defined in main window. How can I modify the collection in the new window so that the commented part would work?

This is my code:

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Pierwszy_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<string> pplList = new ObservableCollection<string>();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Aktywuj(object sender, RoutedEventArgs e)
        {
            Window1 secondWindow = new Window1();
            secondWindow.Show();
        }
    }
}

MainWindow.xaml

<Window x:Class="Pierwszy_WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Pierwszy_WPF"
        mc:Ignorable="d"
        Title="My program" Height="350" Width="525" Icon="Icon.ico">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>
        <Button Name="button" Grid.ColumnSpan="2" Content="Click me!" HorizontalAlignment="Left" Height="100" Margin="315,60.6,0,-110.2" Grid.Row="1" VerticalAlignment="Top" Width="75" Click="Aktywuj"/>



    </Grid>
</Window>

Window1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Pierwszy_WPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void onClick(object sender, RoutedEventArgs e)
        { 
            //pplList.Add("John");
            this.Close();
        }
    }
}

Window1.xaml

<Window x:Class="Pierwszy_WPF.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Pierwszy_WPF"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Button x:Name="button" Content="Click me too!" HorizontalAlignment="Left" Margin="115,241,0,0" VerticalAlignment="Top" Width="75" Click="onClick"/>


    </Grid>
</Window>

One way would be to pass pplList as constructor argument to secondWindow

private void Aktywuj(object sender, RoutedEventArgs e)
{
     var secondWindow = new Window1(pplList);
     secondWindow.Show();
}

Then you would have to add a parameter to Window1 constructor, and a field to store the observable collection, like below

public partial class Window1 : Window
{
    private ObservableCollection<string> _pplList;

        public Window1(ObservableCollection<string> ppList)
        {
            _ppList=ppList;
            InitializeComponent();
        }

Please note that I made the _ppList field private variable, as public variables is not a good practice since they break encapsulation. It's recommended to use the Encapsulate Field refactoring and wrap it in a property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM