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

C#WPF在新窗口中将对象添加到集合中,c#,wpf,xaml,C#,Wpf,Xaml,所以我开始我的冒险与WPF,我想做一个简单的应用程序,有两个窗口。有一个按钮可以触发一个新窗口,在这个窗口中可以选择向myObservableCollection添加一个新对象。我设法创建了两个窗口,但是在创建了新窗口之后,新的.cs文件没有看到在主窗口中定义的集合。如何在新窗口中修改集合,以便注释的部分可以工作 这是我的代码: MainWindow.xaml.cs using System; using System.Collections.Generic; using System.Coll

所以我开始我的冒险与WPF,我想做一个简单的应用程序,有两个窗口。有一个按钮可以触发一个新窗口,在这个窗口中可以选择向my
ObservableCollection
添加一个新对象。我设法创建了两个窗口,但是在创建了新窗口之后,新的
.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();
        }
    }
}
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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
名称空间Pierwszy_WPF
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
public ObservableCollection pplList=新的ObservableCollection();
公共主窗口()
{
初始化组件();
}
私有void-Aktywuj(对象发送方,RoutedEventArgs e)
{
Window1 secondWindow=新Window1();
secondWindow.Show();
}
}
}
main window.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>
<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>

Window1.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();
        }
    }
}
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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
名称空间Pierwszy_WPF
{
/// 
///Window1.xaml的交互逻辑
/// 
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
}
private void onClick(对象发送方,RoutedEventArgs e)
{ 
//pplList.Add(“John”);
这个。关闭();
}
}
}
Window1.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>
<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>

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

private void Aktywuj(object sender, RoutedEventArgs e)
{
     var secondWindow = new Window1(pplList);
     secondWindow.Show();
}
然后,您必须向Window1构造函数添加一个参数,并添加一个字段来存储可观察的集合,如下所示

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

        public Window1(ObservableCollection<string> ppList)
        {
            _ppList=ppList;
            InitializeComponent();
        }
公共部分类窗口1:窗口
{
私人可观察收集;
公共窗口1(ObservableCollection应用程序)
{
_ppList=ppList;
初始化组件();
}

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

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

private void Aktywuj(object sender, RoutedEventArgs e)
{
     var secondWindow = new Window1(pplList);
     secondWindow.Show();
}
然后,您必须向Window1构造函数添加一个参数,并添加一个字段来存储可观察的集合,如下所示

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

        public Window1(ObservableCollection<string> ppList)
        {
            _ppList=ppList;
            InitializeComponent();
        }
公共部分类窗口1:窗口
{
私人可观察收集;
公共窗口1(ObservableCollection应用程序)
{
_ppList=ppList;
初始化组件();
}

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

执行此操作后,可视化显示“Window1不包含带1个参数的构造函数”。我必须修改任何其他内容才能工作吗?使用此代码,两个变量都指向ObservaleCollection的同一个对象实例,因此,是的,您将能够观察添加和删除-只要您不将应用程序分配给ObservaleCollection的新实例,不必客气,请不要忘记标记为答案:-)Exc使用我来完成主题,但是有没有办法限制ListView中显示的位置的数量?我不想显示集合中的所有元素,我只想列出其中的3个元素,当然,答案是:)我不知道。您可以使用LINQ并执行类似于
var\u filteredpllist=new observecollectio的操作n(_ppList.OrderBy(x=>x).Take(3))
然后将ListView绑定到此集合。这将阻止来自原始_ppList的更新,因此要进行更新,您必须订阅_ppList.CollectionChanged事件并在事件处理程序中运行上面的代码。执行此可视化操作后“Window1不包含接受1个参数的构造函数"。我必须修改任何其他内容才能工作吗?使用此代码,两个变量都指向ObservaleCollection的同一个对象实例,因此,是的,您将能够观察添加和删除-只要您不将应用程序分配给ObservaleCollection的新实例,不必客气,请不要忘记标记为答案:-)Exc使用我来完成主题,但是有没有办法限制ListView中显示的位置的数量?我不想显示集合中的所有元素,我只想列出其中的3个元素,当然,答案是:)我不知道。您可以使用LINQ并执行类似于
var\u filteredpllist=new observecollectio的操作n(_ppList.OrderBy(x=>x).Take(3));
然后将ListView绑定到此集合。这将阻止来自原始_ppList的更新,因此要使其更新,您必须订阅_ppList.CollectionChanged ev