阻止用户选择选项卡 WPF 选项卡项
我必须阻止用户在 WPF TabControl 中选择 tabitem,
1)除非用户在一种情况下选中复选框,否则应向用户显示一个消息框,如果选中该复选框,他可以导航到任何其他选项选项卡
2)检查特定条件,用户在选择它时不应该能够进入特定选项卡,并且我没有使选项卡项目折叠的选项。并且它应该弹出一个消息框并返回到相同的prv选择的选项卡项目
我已经看到了 Smith Josh 的示例代码,如下所示,这正是我想要的第一个场景
http://joshsmithonwpf.wordpress.com/2009/09/04/how-to-prevent-a-tabitem-from-being-selected/
但我需要一些可以在 MVVM 中工作的东西,其中我的应用程序有严格的“无代码隐藏”
I had to prevent the user from selecting a tabitem in a WPF TabControl,
1)unless and untill the user checks a check box in one condition the user should be shown a message box and if he checks the check box he can navigate to any other tab
2)Checking a particular condition the user shouldnt be able to get into a particular tab on selecting it,and I dont have an option of making the tab item collapse.and it should pop up a message box and get back to the same prv tab item selected
I have seen Smith Josh's sample code as below and this is what i exactly wanted for the 1st scenerio
http://joshsmithonwpf.wordpress.com/2009/09/04/how-to-prevent-a-tabitem-from-being-selected/
But I need something that works in MVVM, where my application has a strict "No CodeBehind"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以继承 TabControl (或添加附加属性),它控制是否允许导航到另一个选项卡项;然而,让我强调一下,“没有代码隐藏”有点愚蠢——很多时候代码隐藏可以用于仅供查看的目的,这没关系。
回到问题...使用我的建议您要做的是将隐藏代码(检查是否允许操作)隐藏在控件内,以便实际视图(页面/窗口等)不包含它。如果您将新属性声明为 DependencyProperty,您将获得所有绑定设施等。
You could inherit the TabControl (or add an attached property) which controls if navigation to another tab item is allowed; however, let me just stress that 'no codebehind' is kinda silly - there are plenty of times when code-behind can be used for view-only purposes, and that's ok.
Back to the problem... what you'd do using my suggestion is hide the code-behind (checking if the action is allowed) inside a control, so that the actual view (the page/window etc) doesn't contain it. If you declare the new property as a DependencyProperty you get all the binding facilities etc.
编辑:我测试了其他代码,但它不起作用。无论如何,这只是一个想法。这是一个确实有效的方法(尽管我同意 Alex 的观点,即在调整视图时,MVVM 中的代码很好)。
在本例中,我创建了一个转换器,它采用两个布尔值:是否选择选项卡以及是否可以更改选项卡。如果这两者都设置为 false,我们将返回 false 以禁用该选项卡。如果其中一个设置为 true,我们将启用该选项卡。
这是代码。我的 VM 中有一个名为 CanChangeTabs 的属性,并且 Window.Resources 中有一个名为 Converter 的 MyConverter 实例。
XAML inTabItem:
转换器:
EDIT: I tested my other code and it didn't work. Was just an idea anyways. Here's a method that does work (although I agree with Alex that code behind in MVVM is fine when adjusting the View).
In this case I created a converter which takes two boolean values: If the tab is selected and if we can change tabs. If both of these are set to false, we return false to disable the tab. If either is set to true, we leave the tab enabled.
Here's the code. I have a property in my VM called CanChangeTabs and an instance of MyConverter in Window.Resources called Converter.
XAML inTabItem:
Converter: