当前位置: 首页 > 编程语言 > C#

更改DataGrid时,使用MessageBox添加一个检查分享

时间:2023-04-11 11:28:17 C#

C#学习教程:更改DataGrid时使用MessageBox添加检查我将UpdateEnabled用于不同的东西(配置文件权限),我也不希望DataGrid是只读的:我更愿意(除非它太复杂)看到阻止更改的阻止警报(MessageBox).到目前为止,我所做的是与MVVM作斗争,因为我已经将警报放入模型中(但如果它使更改变得快速和容易,我可以接受)不起作用,因为我更改的第二部分(见下文))产生无效操作异常ViewModel包含以下列表[Association(ThisKey="Partita",OtherKey="Partita",CanBeNull=true,IsBackReference=true)]publicObservableCollectionposin_locations_list=newObservableCollection();公共ObservableCollectionPosInLocationsList{get{返回posin_locations_list;}set{posin_locations_list=value;OnPropertyChanged(()=>PosInLocationsList);我在这里添加了一致性检查string_storage;[Column(Name="storage"),PrimaryKey]publicstringStorage{get{return_storage;}set{if(this.loadedEF){stringvalidate_msg;if(!PurchasePosIn.CanBeDeleted(outvalidate_msg)){//针对MVVMMessageBox.Show(validate_msg,"Alert",MessageBoxButton.OK);OnPropertyChanged(()=>存储);返回;}Persistence.MyContext.deletePosInLocation(this);}_storage=值;OnPropertyChanged(()=>存储);如果(this.loadedEF){Persistence.MyContext。insertPosInLocation(这个);}}}这里(第二部分)internalvoidposin_locations_list_CollectionChanged(objectsender,System.Collections.Specialized.NotifyCollectionChangedEventArgsargs){stringvalidate_msg;if(!CanBeDeleted(outvalidate_msg)ly){//间接无效操作异常MessageBox.Show(validate_msg,"Alert",MessageBoxButton.OK);返回;}if(args.OldItems!=null)foreach(varoldIteminargs.OldItems){if(((PosInLocation)oldItem).Partita!=null)Persistence.MyContext.deletePosInLocation((PosInLocation)oldItem);}if(args.NewItems!=null)foreach(args.NewItems中的varnewItem){PosInLocationnewPosInLocation=(PosInLocation)newItem;如果(newPosInLocation.Partita==null){newPosInLocation.Partita=this.Partita;newPosInLocation.PurchasePosIn=this;newPosInLocation.loadedEF=true;如果只有ObservableCollection实现了“previewCollectionChanged”,事情就会如此简单根据您的需要,我建议创建ObservableCollection的子类并重载受保护的方法RemoveItem。根据您对应用程序执行的操作,您可能希望覆盖RemoveItem以外的方法(例如ClearItems)。在继承ObservableCollection时,可以覆盖5个受保护的方法:ClearItems、RemoveItem、InsertItem、SetItem和MoveItem。最后,所有公共方法都会调用这些方法,因此重写它们可以让您完全控制。这是一个用于演示的小型工作应用程序:ObservableCollectionSubClass公共类ObservableCollectionWithDeletionControl:公共事件DeletionDeniedEventHandlerDeletionDenied;公共事件DeletionDeniedEventHandlerDeletionDenied;voidOnDeletionDenied(intindexOfDeniedDeletion){if(DeletionDenied!=null){DeletionDenied(this,indexOfDeniedDeletion);}}protectedoverridevoidRemoveItem(intindex){if(CanDelete){base.RemoveItem(index);}else{OnDeletion索引);}}}我使用DeletionDenied事件,这样这个类不负责显示错误窗口并使其更可重用。}=newObservableCollectionWithDeletionControl();publicMainWindowViewModel(){People.DeletionDenied+=People_DeletionDeniedDenied;intindexOfDeniedDeletion){PersonpersonSavedFromDeletion=People[indexOfDeniedDeletion];window.displayDeniedDeletion(personSavedFromDeletion.Name);}}MainWindow的ViewModel。它知道它的窗口仅用于显示错误消息。(我确信有比这更好的解决方案,但我还没有找到在mvvm中显示弹出窗口的好方法。)当DeletionDenied事件触发时调用错误窗口。模型publicclassPerson:INotifyPropertyChanged{publiceventPropertyChangedEventHandlerPropertyChanged;publicstringName{get{return_name;}set{if(_name==value){返回;}_name=值;if(PropertyChanged!=null){PropertyChanged(this,newPropertyChangedEventArgs("Name"));}}}私有字符串_name="";}XAMLXAML.CSpublicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();}publicvoiddisplayDeniedDeletion(stringname){TextBoxerrorMessage=newTextBox();errorMessage.Text=string.Format("无法删除{0}:访问被拒绝!",name);窗口popupErrorMessage=newWindow();popupErrorMessage.Content=errorMessage;popupErrorMessage.ShowDialog();}}APPMAINpublicpartialclassApp:Application{privatevoidApplication_Startup(objectsender,StartupEventArgse){MainWindowwindow=newMainWindow();MainWindowViewModelviewModel=newMainWindowViewModel();视图Model.window=窗口;window.DataContext=viewModel;窗口.显示();App.Current.MainWindow=窗口;我在启动时设置了ViewModel的窗口,但是你应该在创建ViewModel的任何地方都这样做也许它很丑陋(真的,它并没有那么丑陋:恕我直言,这是一个很好的MVVM方法,也适用于现代mahapps.metro对话框),但是正确的现在我正在设置if(!CanBeDeleted(outvalidate_msg)){PurchaseItem.MessageBoxText=validate_msg;一个不可见的TextBox我应该在哪里发送警报voidTextBox_TextChanged(objectsender,TextChangedEventArgse){if(alert!=null&&tb_message.Text.Length>0){Dispatcher.BeginInvoke((Action)(()=>{MessageBox.Show(alert,"Alert",MessageBoxButton.OK);tb_message.Text="";}));回滚添加/删除的项目我看到另一个问题的连接阻止在ObservableCollection.CollectionChanged事件上添加新项目,在我的情况下我会说防止删除更重要我不知道是否有比更新的答案这个(CanIrollbackcollectionchangeoncollectionchangeevent?这看起来不对)关于这个话题。虽然可以很容易地引发PropertyChanged以回滚项目更新,但对于集合更改,我不得不在CollectionChanged事件PurchaseItem.dispatcher.BeginInvoke((Action)(()=>RollBack(args)))中传递和引用视图调度程序;回滚添加/删除的项目boolrollingBack=false;privatevoidRollBack(NotifyCollectionChangedEventArgsargs){rollingBack=true;if(args.Action==NotifyCollectionChangedAction.Remove){foreach(args.OldItems中的var元素){PosInLocationsList.Add((PosInLocation)element);}}if(args.Action==NotifyCollectionChangedAction.Add){foreach(args.NewItems中的var元素){PosInLocationsList.Remove((PosInLocation)element);}}rollingBack=false;到目前为止,我所做的是反对MVVM,因为我已将警报放入模型来自@Tesseract、inheritanceObservableCollection和订阅RemoveItem的解决方案已经面向MVVM。仍然缺少的是一种从ViewModel发送警报的正确、现代的方法。这就是Mahapps方法的用武之地。在您的XAML对话框中:DialogParticipation.Register="{Binding}"附加属性DialogPartecipation将通过字典跟踪视图publicstaticclassDialogParticipation{privatestaticreadonlyIDictionaryContextRegistrationIndex=newDictionaryDialogCoordinatorMatchViewModeltoView只读IDialogCoordinator实例=newDialogCoordinator();通过上面提到的附加属性(上下文是视图模型)varassociation=DialogParticipation.GetAssociation(context);并显示对话框,在检索到的视图上调用适当的方法:如果打开多个窗口,对话框将显示在正确的窗口上。以上是C#学习教程:改变DataGrid时,使用MessageBox加勾,共享所有内容。如果对大家有用,需要了解更多C#学习教程,希望大家多多关注---本文来自网络合集,不具有代表性如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢