2016-04-22 10 views
0

私はWPFで冒険を始めています.2つのウィンドウを持つ単純なアプリケーションを作りたかったのです。 1つは、私のObservableColletionに新しいオブジェクトを追加するオプションがある新しいウィンドウを起動するボタンを持っています。そして、私は2つのウィンドウを作成することができましたが、新しいウィンドウを作成した後、新しい.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

一つの方法は、次にあなたがウィンドウ1のコンストラクタにパラメータを追加する必要がありますsecondWindow

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

にコンストラクタの引数としてpplListを渡すこと、およびでしょう以下のように観測可能なコレクションを格納するフィールド。

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

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

_ppListフィールドをプライベートに変更しました。パブリック変数はカプセル化を破るので、パブリック変数は適切ではありません。カプセル化フィールドのリファクタリングを使用してプロパティにラップすることをお勧めします。

+0

このビジュアルを実行すると、「Window1には1つの引数を取るコンストラクタが含まれていません」というメッセージが表示されます。これが機能するには他の何かを修正する必要がありますか? –

+0

確かに私のコードをちょうど1秒で更新させてください – ironstone13

+0

このコードでは、両方の変数がObservableCollectionの同じオブジェクトインスタンスを指しているので、追加と削除を監視することができます - ppListを徹底しない限りObservableCollectionの新しいインスタンス – ironstone13

関連する問題