在UserControl中刷新数据网格

时间:2018-05-08 11:02:28

标签: c# wpf datagrid

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

我有2个窗口(一个带有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)
        {
        }

类包含

 class Reservation
{
    public string movieName { get; set; }
    public int seat { get; set; }
    public string rowID { get; set; }
}

Window2具有带数据网格的UserControl。

你能否给我一些建议如何将window2 usercontrol datagrid绑定到window1中的列表,当我点击window1中的ADD按钮时,刷新该usercontrol并在window1列表中显示实际位置。

我希望您的理解并提前感谢您!

2 个答案:

答案 0 :(得分:1)

为了反映集合中的更改,您的集合应该是Observable集合。请参阅此文章如何使用可观察集合:https://www.c-sharpcorner.com/UploadFile/e06010/observablecollection-in-wpf/

这个问题是一个很好的例子,可以用来查看数据网格绑定:MVVM datagrid binding 当您将新预订添加到您的reservationList(您制作Observable)时,更改将反映在UI中。我希望这会有所帮助。

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

答案 1 :(得分:1)

我不明白为什么你需要两个窗户,所以我假设你可以通过一个窗口来看看。你不是这么说的,但我假设用户会将电影名称,座位号和行ID输入到窗口的文本框中。

要回答您的第一个问题,只需将列表绑定到DataGrid即可将列表分配给DataGrid ItemsSource属性。例如(参见下面的MainWindow方法):

dataGridReservations.ItemsSource = reservations.List;

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

您希望将预留列表实现为ObservableCollection,因为ObservableCollection会在绑定列表中添加,删除或修改项目时自动将更改传播到数据网格。见How do I bind a List to a WPF DataGrid?

对于第二个问题:使用Add按钮单击事件将文本框中的电影名称,座位编号和行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 = "";
    }
}

MainWindow XAML

&#13;
&#13;
    <Window x:Class="SO_Refresh_datagrid.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:SO_Refresh_datagrid"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Button Content="Add" HorizontalAlignment="Left" Height="30" Margin="83,169,0,0" VerticalAlignment="Top" Width="64" Click="ButtonAddReservationToList"/>
            <TextBox x:Name="textBoxMovieName" HorizontalAlignment="Left" Height="31" Margin="140,18,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="103"/>
            <TextBox x:Name="textBoxSeatNumber" HorizontalAlignment="Left" Height="24" Margin="140,61,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="103"/>
            <TextBox x:Name="textBoxRowId" HorizontalAlignment="Left" Height="24" Margin="140,100,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="103"/>
            <Label Content="Movie Name" HorizontalAlignment="Left" Height="31" Margin="36,18,0,0" VerticalAlignment="Top" Width="71"/>
            <Label Content="Seat Number" HorizontalAlignment="Left" Height="31" Margin="36,61,0,0" VerticalAlignment="Top" Width="90"/>
            <Label Content="Row Id" HorizontalAlignment="Left" Height="31" Margin="36,100,0,0" VerticalAlignment="Top" Width="71"/>
            <DataGrid x:Name="dataGridReservations" HorizontalAlignment="Left" Height="284" Margin="277,18,0,0" VerticalAlignment="Top" Width="209"/>
    
        </Grid>
    </Window>
    
&#13;
&#13;
&#13;

<强>演示

enter image description here