2016-04-22 68 views
0

所以我开始我的WPF冒险,我想做一个简单的应用程序,有2个窗口。一个会有一个按钮触发一个新的窗口出现,其中将有一个选项可以添加一个新对象到我的ObservableColletion。我设法创建了两个窗口,但创建新窗口后,新的.cs文件没有看到在主窗口中定义的集合。我如何在新窗口中修改集合,以便评论部分可以工作?C#WPF在新窗口中添加对象到集合

这是我的代码:

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> 

回答

3

一种方式是通过pplList作为构造参数secondWindow

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

那么你就必须添加一个参数的构造函数窗口1,和字段来存储可观察的集合,如下所示

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

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

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

+0

做完这个视觉后,说“Window1不包含一个带1个参数的构造函数”。我是否必须修改其他任何内容才能使用? –

+0

当然,让我更新我的代码,只需一秒 – ironstone13

+0

有了这段代码,两个变量都指向ObservableCollection的同一个对象实例,所以是的,你将能够观察到添加和删除 - 只要你不沾ppList到ObservableCollection的新实例 – ironstone13

相关问题