开发者

Best way to extend the default behavior of some TabControl

开发者 https://www.devze.com 2023-04-05 18:24 出处:网络
I have a Custom TabControl in a WPF application (attention: the technology is not important as it may change) that contains TabItems. Each TabItem has a text, and a \'x\' button to close the tab.

I have a Custom TabControl in a WPF application (attention: the technology is not important as it may change) that contains TabItems. Each TabItem has a text, and a 'x' button to close the tab.

The request is to add some features to the close functionality in order to change it's default behavior (the simple closing). What I need to do is simply to check if some data displayed inside the screen have changed, and if so to prompt the user with some MessageBox to inform him that some changes were made in that tab.

The MessageBox will display the message "Some changes have been made to the data. Do you want to save them?", and will have the options:

  • Yes - Save Changes then close tab;
  • No - Do not save them and close tab;
  • Cancel - Do nothing, do not save, do not close;

There is no issue with detecting if changes have been made to the scree, due to the fact that I simply set some property HasChanged whenever any change has been made to the data.

For now, the only solution I thought of was to extend the Control, and implement a new close event. This solution will do for now, because I have to do only one change to the tabs behavior. The problem is that I might need to extend the functionality in other way in the future, and I don't want to extend the TabControl ev开发者_运维问答ery time the request is changing.

Also I need to keep the default behavior of the TabItem (the simple close).

Can anybody point me out to some optimum solution, to be easily extended with different behaviors, and also portable on any .NET application.

Thank you.


Given that you already have defined a custom control, then I would suggest adding a new event such as OnTabClosing which is fired before your custom control closes the tab. This way you can add custom functionality to a tab being closed, without having to keep extend the control itself. The link should provide all the information you need to get started with that.

You can also take a look at Routed Events if your interested in making this available within XAML for storyboard animations as well.


I suggest that you bind the close button to a Command. The command would then call your logic and show the message box.

I think that this would be cleaner than to use events because there is always some overhead in managing the publishers and subscribers.

Using the Command gives you flexible control over where you put your logic and whose functions you want to call.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号