C# WPF技术咨询:添加预订后如何刷新UserControl中的DataGrid
看起来你遇到的核心问题是普通List<T>不会自动通知UI更新,再加上窗口和UserControl之间的数据源共享需要妥善处理。我给你一套实用的解决方案,步骤很清晰:
第一步:把List换成ObservableCollection
WPF的List<T>没有实现集合变更通知接口,所以添加/删除元素时UI不会自动刷新。换成ObservableCollection<T>就搞定了——它自带INotifyCollectionChanged接口,集合变化时会主动通知DataGrid更新。
修改Window1里的定义:
// 把原来的List换成ObservableCollection,声明为私有字段+公共属性方便绑定 private ObservableCollection<Reservation> _reservationList = new ObservableCollection<Reservation>(); public ObservableCollection<Reservation> ReservationList => _reservationList;
然后在Window1的构造函数里设置自身为DataContext(方便后续绑定):
public Window1() { InitializeComponent(); DataContext = this; }
第二步:让UserControl能接收这个集合
我们给UserControl加一个依赖属性,用来接收Window1传递过来的Reservation集合。这样两者共享同一个数据源,集合变化时UserControl的DataGrid会自动感知。
在你的UserControl代码后台(比如叫ReservationUserControl.xaml.cs)添加:
public partial class ReservationUserControl : UserControl { // 定义依赖属性,让外部可以绑定 public static readonly DependencyProperty ReservationsProperty = DependencyProperty.Register( "Reservations", typeof(ObservableCollection<Reservation>), typeof(ReservationUserControl), new PropertyMetadata(null)); public ObservableCollection<Reservation> Reservations { get => (ObservableCollection<Reservation>)GetValue(ReservationsProperty); set => SetValue(ReservationsProperty, value); } public ReservationUserControl() { InitializeComponent(); } }
第三步:绑定UserControl的DataGrid到集合
在UserControl的XAML里,把DataGrid的ItemsSource绑定到我们刚定义的Reservations属性:
<DataGrid ItemsSource="{Binding Reservations}" AutoGenerateColumns="False" CanUserAddRows="False"> <DataGrid.Columns> <DataGridTextColumn Header="电影名" Binding="{Binding MovieName}" /> <DataGridTextColumn Header="座位号" Binding="{Binding SeatNumber}" /> <DataGridTextColumn Header="行ID" Binding="{Binding RowId}" /> </DataGrid.Columns> </DataGrid>
第四步:在Window1里关联UserControl和集合
回到Window1的XAML,引用UserControl的时候,把Reservations属性绑定到Window1的ReservationList:
<!-- 假设你的UserControl命名空间是local --> <local:ReservationUserControl Reservations="{Binding ReservationList}" />
第五步:修改ToggleButton事件逻辑
现在你只需要在ToggleButton_Checked事件里直接往_reservationList加元素就好,ObservableCollection会自动通知UI刷新:
private void ToggleButton_Checked(object sender, RoutedEventArgs e) { var toggleBtn = sender as ToggleButton; if (toggleBtn == null) return; var hall = toggleBtn.DataContext as Hall; if (hall == null) return; var newReservation = new Reservation { MovieName = "这里填你的电影名(比如从窗口控件绑定获取)", SeatNumber = hall.SeatNumber, // 假设Hall有这个属性 RowId = hall.RowId // 假设Hall有这个属性 }; _reservationList.Add(newReservation); }
可选:让Reservation属性变化也能刷新UI
如果后续你需要修改Reservation的属性(比如改电影名)也让DataGrid同步更新,那Reservation类需要实现INotifyPropertyChanged接口:
public class Reservation : INotifyPropertyChanged { private string _movieName; public string MovieName { get => _movieName; set { if (_movieName != value) { _movieName = value; OnPropertyChanged(nameof(MovieName)); } } } private int _seatNumber; public int SeatNumber { get => _seatNumber; set { if (_seatNumber != value) { _seatNumber = value; OnPropertyChanged(nameof(SeatNumber)); } } } private int _rowId; public int RowId { get => _rowId; set { if (_rowId != value) { _rowId = value; OnPropertyChanged(nameof(RowId)); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
这样操作后,你每次勾选ToggleButton添加Reservation时,UserControl里的DataGrid都会自动刷新显示新的条目啦!
内容的提问来源于stack exchange,提问作者Danki

