C#WPF在新窗口中将对象添加到集合中

时间:2016-04-22 08:19:43

标签: c# wpf xaml

所以我开始使用WPF进行冒险,我想制作一个有2个窗口的简单应用程序。可以有一个触发新窗口的按钮,其中可以选择向我的ObservableColletion添加新对象。我设法创建了两个窗口,但在创建新窗口后,新的.cs文件看不到主窗口中定义的集合。如何在新窗口中修改集合,以便注释部分可以工作?

这是我的代码:

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>

1 个答案:

答案 0 :(得分:3)

一种方法是将pplList作为构造函数参数传递给secondWindow

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

然后你必须向Window1构造函数添加一个参数,以及一个存储observable集合的字段,如下所示

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

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

请注意,我创建了_ppList字段私有变量,因为公共变量不是一个很好的做法,因为它们打破了封装。建议使用Encapsulate Field重构并将其包装在属性中。