2016-04-22 44 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