确认在选项卡之间切换 [英] Confirmation on switching between tabs

查看:70
本文介绍了确认在选项卡之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选项.用户将输入一些数据并将其保存在第一个选项卡上.第二个选项卡列出了保存的数据.我需要的是,当用户在将数据保存到第一个选项卡中之前选择了第二个选项卡时,将显示一个确认消息框,显示是,否和取消.如果用户单击是"按钮,则应保存数据并转到第二个选项卡.如果他单击否,则无需保存数据,最后,如果单击取消,该选项卡将保留所有输入的数据.我该怎么做?

I have two tabitems. User will enter some data and save it on the first tab. The second tab lists the saved data. What I need is when the user select the second tab before saving data in first tab a confirmation message box with Yes, No and Cancel should be shown. If the user clicks Yes button the data should be saved and go to the second tab. If he hits No, the data need not to be saved and finally if Cancel is hit the tab will retain with all entered data. How can i make this?

推荐答案

为简单起见,您可以在隐藏代码"文件中进行以下操作.

To keep things simple you can do the follwing in the Code Behind file.

我将创建要在WPF控件中显示和编辑的数据的Model类.使模型实现 INotifyPropertyChanged IEditableObject 接口.

I'd create a Model class of the data you want to display and edit in the WPF Control. Make the Model implement the INotifyPropertyChanged and IEditableObject interfaces.

INotifyPropertyChanged 将允许您绑定到模型. IEditableObject 将允许您提供编辑",保存"和取消"功能.

INotifyPropertyChanged will allow you to Bind to the Model. IEditableObject will allow you to provide Edit, Save and Cancel functionality.

TabControl具有一个您可以处理的 SelectionChanged 事件,该事件可让您检测用户何时更改选项卡,在此处理程序中,您可以使用 System.Windows.MessageBox 要求用户保存等, System.Windows.MessageBox.Show()返回一个 MessageBoxResult 对象,您可以使用该对象确定用户单击了什么按钮并执行适当的操作

The TabControl has a SelectionChanged Event you can handle, that will allow you to detect when the user changes tabs, in this handler you can use System.Windows.MessageBox to ask the user to save etc, System.Windows.MessageBox.Show() returns a MessageBoxResult Object you can use to detirmine what button the user clicked and perform the appropiate action.

这不是完成任务的好方法,但是它使事情变得简单,您可能需要研究一些WPF设计模式以帮助实现代码可管理性.

This is not a geat way to do things, but it keeps things simple, you may want to look into some WPF design Patterns to help with Code Manageability.

如果您需要进一步说明,请询问.

If you need anything explained further, just ask.

这篇关于确认在选项卡之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆