C# 在UserControl中刷新数据网格

C# 在UserControl中刷新数据网格,c#,wpf,datagrid,C#,Wpf,Datagrid,我正在学习c#和wpf,我不知道如何处理这样的事情: 我有两个窗口(一个带有usercontrol)和一个类 窗口1 List<Reservation> reservationList = new List<Reservation>(); private void ToggleButton_Checked(object sender, RoutedEventArgs e) { var button = (Toggle

我正在学习c#和wpf,我不知道如何处理这样的事情:

我有两个窗口(一个带有usercontrol)和一个类

窗口1

    List<Reservation> reservationList = new List<Reservation>();
    private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            var button = (ToggleButton)sender;
            var item = button.DataContext as Hall;
            Reservation nres = new Reservation();
            nres.movieName = item.moviename;
            nres.seat = item.number;
            nres.rowID = item.row;
            reservationList.Add(nres);
        }

        private void Add_Button_Click(object sender, RoutedEventArgs e)
        {
        }
Window2具有带有datagrid的UserControl

您能给我一些建议,如何将window2用户控件数据网格绑定到window1中的列表中,当我在window1中点击ADD按钮时,它会刷新该用户控件并在window1列表中显示实际位置


我希望你能理解,并提前感谢你

为了反映集合中的更改,您的集合应该是可观察的集合。请参阅本文如何使用可观察集合:

这个问题是可以用来查看datagrid绑定的一个很好的示例: 当您将一个新的预订添加到您的预订列表(您使其可见)时,更改将反映在UI中。我希望这会有所帮助


当您使用WPF时,应该遵循MVVM模式,因为它将使您的生活更轻松。请参阅此处的一些教程链接:我希望这会有所帮助。

为了反映集合中的更改,您的集合应该是可观察的集合。请参阅本文如何使用可观察集合:

这个问题是可以用来查看datagrid绑定的一个很好的示例: 当您将一个新的预订添加到您的预订列表(您使其可见)时,更改将反映在UI中。我希望这会有所帮助


当您使用WPF时,应该遵循MVVM模式,因为它将使您的生活更轻松。请参阅此处的一些教程链接:我希望这会有所帮助。

我不明白您为什么需要两个窗口,因此我假设您可以使用一个窗口。您没有这么说,但我假设用户将在窗口的文本框中输入电影名称、座位号和行Id

要回答第一个问题,将列表绑定到
DataGrid
只需将列表分配给
DataGrid
项资源
属性即可。例如(请参见下面的
main窗口
方法):

dataGridReservations.ItemsSource=reservations.List

我是WPF新手,但似乎
DataGrid
的默认行为是从列表中变量的名称创建列名

您希望将保留列表实现为
observedcollection
,因为
observedcollection
会在绑定列表中添加、删除或修改项目时自动将更改传播到数据网格。看

关于第二个问题:使用
添加
按钮单击事件,将文本框中的电影名称、座位号和行Id添加到列表中的新项目中。同样,当列表被更新时,
DataGrid
由于
ObservableCollection

以下代码允许用户输入电影名称、座位号和行id,然后单击添加按钮将其添加到
DataGrid
。需要更多的代码来允许用户编辑或删除网格中的项目

XAML遵循代码

请参见底部的屏幕截图以获取演示

public partial class MainWindow : Window
{

    Reservations reservations;

    public MainWindow()
    {
        InitializeComponent();

        reservations = new Reservations();

        dataGridReservations.ItemsSource = reservations.List;
    }

    public class Reservations
    {
        public class Reservation
        {
            private string _movieName;
            private string _seat;
            private string _rowID;

            public Reservation(string movieName, string seat, string rowID)
            {
                MovieName = movieName;
                Seat = seat;
                RowID = rowID;
            }

            public string MovieName { get => _movieName; set => _movieName = value; }
            public string Seat { get => _seat; set => _seat = value; }
            public string RowID { get => _rowID; set => _rowID = value; }
        }

        private ObservableCollection<Reservation> _list;

        public ObservableCollection<Reservation> List { get => _list; set => _list = value; }

        public Reservations()
        {
            List = new ObservableCollection<Reservation>();
        }

        public void AddReservationToList(string MovieName, string SeatNumber, string RowId)
        {
            Reservation reservation = new Reservation(MovieName, SeatNumber, RowId);
            List.Add(reservation);
        }

    }

    private void ButtonAddReservationToList(object sender, RoutedEventArgs e)
    {
        reservations.AddReservationToList(textBoxMovieName.Text, textBoxSeatNumber.Text, textBoxRowId.Text);
        textBoxMovieName.Text = "";
        textBoxSeatNumber.Text = "";
        textBoxRowId.Text = "";
    }
}
公共部分类主窗口:窗口
{
保留;
公共主窗口()
{
初始化组件();
保留=新保留();
dataGridReservations.ItemsSource=reservations.List;
}
公务舱预订
{
公务舱预订
{
私有字符串_movieName;
私人座位;
私有字符串_rowID;
公共预订(字符串movieName、字符串seat、字符串rowID)
{
MovieName=MovieName;
座位=座位;
RowID=RowID;
}
公共字符串MovieName{get=>\u MovieName;set=>\u MovieName=value;}
公共字符串Seat{get=>\u Seat;set=>\u Seat=value;}
公共字符串RowID{get=>\u RowID;set=>\u RowID=value;}
}
私有可观察收集列表;
公共ObservableCollection列表{get=>\u List;set=>\u List=value;}
公众保留()
{
列表=新的ObservableCollection();
}
public void AddReservationToList(字符串MovieName、字符串SeatNumber、字符串RowId)
{
预订预订=新预订(MovieName、SeatNumber、RowId);
列表。添加(预订);
}
}
私有无效按钮ADDREServationToList(对象发送方,路由目标)
{
reservations.AddReservationToList(textBoxMovieName.Text、textBoxSeatNumber.Text、textBoxRowId.Text);
textBoxMovieName.Text=”“;
textBoxSeatNumber.Text=”“;
textBoxRowId.Text=“”;
}
}
主窗口XAML


我不明白你为什么需要两个窗口,所以我假设你只需要一个窗口就可以了。您没有这么说,但我假设用户将在窗口的文本框中输入电影名称、座位号和行Id

要回答第一个问题,将列表绑定到
DataGrid
只需将列表分配给
DataGrid
项资源
属性即可。例如(请参见下面的
main窗口
方法):

dataGridReservations.ItemsSource=reservations.List

我是WPF新手,但似乎
DataGrid
的默认行为是从列表中变量的名称创建列名

您希望将保留列表实现为
observedcollection
,因为
observedcollection
会在绑定列表中添加、删除或修改项目时自动将更改传播到数据网格。看见
public partial class MainWindow : Window
{

    Reservations reservations;

    public MainWindow()
    {
        InitializeComponent();

        reservations = new Reservations();

        dataGridReservations.ItemsSource = reservations.List;
    }

    public class Reservations
    {
        public class Reservation
        {
            private string _movieName;
            private string _seat;
            private string _rowID;

            public Reservation(string movieName, string seat, string rowID)
            {
                MovieName = movieName;
                Seat = seat;
                RowID = rowID;
            }

            public string MovieName { get => _movieName; set => _movieName = value; }
            public string Seat { get => _seat; set => _seat = value; }
            public string RowID { get => _rowID; set => _rowID = value; }
        }

        private ObservableCollection<Reservation> _list;

        public ObservableCollection<Reservation> List { get => _list; set => _list = value; }

        public Reservations()
        {
            List = new ObservableCollection<Reservation>();
        }

        public void AddReservationToList(string MovieName, string SeatNumber, string RowId)
        {
            Reservation reservation = new Reservation(MovieName, SeatNumber, RowId);
            List.Add(reservation);
        }

    }

    private void ButtonAddReservationToList(object sender, RoutedEventArgs e)
    {
        reservations.AddReservationToList(textBoxMovieName.Text, textBoxSeatNumber.Text, textBoxRowId.Text);
        textBoxMovieName.Text = "";
        textBoxSeatNumber.Text = "";
        textBoxRowId.Text = "";
    }
}