构建向导的好模式?
我通常使用 TabControl
并以某种方式隐藏选项卡并浏览它们。
我很好奇这样做的其他方法!
I usually use a TabControl
and somehow hide the tabs and navigate through them.
I am curious about other ways of doing this!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
面板
对象,除了第一个面板外,所有面板的
Visible
属性都设置为false
。存储当前面板的索引,并具有可适当更改此索引的“下一个”和“上一个”按钮。更改此索引后,根据需要使相应的面板可见/不可见(在给定时间仅当前面板应可见)。
您还可以处理部分/所有面板上的
IsVisibleChanged
事件,以在用户导航到特定面板时触发某些行为。You could use a collection of
Panel
objects, with all but the first panel having theirVisible
property set tofalse
.Store the index of the current panel, and have "Next" and "Previous" buttons that change this index appropriately. When this index is changed, make the appropriate panels visible/not visible as needed (only the current panel should be visible at a given time).
You can also handle the
IsVisibleChanged
event on some/all of your panels, to trigger certain behaviors to occur when the user navigates to a particular panel.这是一个相当复杂的话题;大约两年前,我构建了一个相当全面的向导控件供内部使用,我记得花了几周的时间才完全正常工作。
基本元素包括:
内容面板和操作面板都使用
ParentControlDesigner
来启用放置控件。内容面板不允许您直接将其拖放到其上,而是允许您将其拖放到活动页面(子面板)上。操作面板还有一个“默认”模式,它创建标准的 4 个按钮(上一个、下一个、取消、完成)。我主要实现了自定义模式,以便可以对其进行换肤,即使用 DevEx 按钮而不是标准 Winforms 按钮。标头基本上是静态的,它是一个
PictureBox
和Label
,可自定义图像、文本和字体。 (默认情况下,文本与页面标题相同,字体是添加粗体样式的控件字体)。然后是 API 公开的一组数据结构:
我为这些步骤组合了一个自定义集合编辑器,如上所述,这又为内容面板创建子面板。每个面板都直接添加到控件集合中,但根据活动步骤属性一次只能看到一个面板。我记得对接似乎从来都不太正常,所以我必须覆盖所有调整大小的方法。我从来没有花时间创建一个智能标签来轻松地在页面之间翻转,但可以在属性网格上选择活动页面(或页面索引)。
然后我还必须包含一大堆钩子,用于在各个页面上插入任何自定义逻辑。如果不发布所有代码,很难真正深入了解这里的细节。
正确的设计和测试是相当耗时的,但我不记得使用过任何高超的设计技巧,只是必须有条不紊地解决问题,如上所述,从各个 UI 元素(运行时和设计时)的角度来看和数据结构以及它们如何交互。
请记住,所有这些都是为了创建可重用的向导组件,因为我们需要为特定应用程序开发大约 10 个向导组件(并且它对于其他项目也很方便)。如果我只需要编写一个快速向导,我可能不会经历所有这些麻烦,我只会做你正在做的事情 - 使用选项卡控件和一些流程面板。或者更好的是,我会使用现成的向导控件,现在许多 Winforms 库(例如 DX 库)中都提供了该控件。
This is a pretty involved topic; I built a fairly comprehensive Wizard control about two years ago for use in house and I remember it taking several weeks to get working exactly right.
The basic elements are:
Both the content panel and action panel make use of the
ParentControlDesigner
to enable dropping controls. The content panel doesn't let you drop directly on it, instead it lets you drop on the active page (sub-panel). The action panel also has a "default" mode where it creates the standard 4 buttons (prev, next, cancel, finish). I mainly implemented the custom mode so I could skin it, i.e. using the DevEx buttons instead of the standard Winforms buttons.The header is basically static, it's a
PictureBox
andLabel
with the image, text, and font customizable. (By default the text is the same as the page title and the font is the control's font with bold style added).Then there are a set of data structures exposed by the API:
I put together a custom collection editor for the steps, which in turn create sub-panels for the content panel as explained above. Each panel is just added straight to the control collection, but only one is ever visible at a time based on the active step property. I remember that docking never seemed to work quite right so I had to override all of the resize methods. I never did get around to creating a smart tag to easily flip between pages, but the active page (or page index) can be chosen on the property grid.
Then I also had to include a whole bunch of hooks for inserting any custom logic on individual pages. Hard to really get into much detail here without posting all the code.
It's pretty time-consuming to properly design and test, but I don't remember using any whizbang design tricks, just had to approach the problem methodically, as mentioned, from the perspective of the individual UI elements (both runtime and design-time) and data structures and how they interact.
Keep in mind all of this was for the purpose of creating a reusable Wizard component because we needed to develop about 10 of them for a particular app (and it's come in handy for other projects as well). If I just needed to hack together one quick wizard, I probably wouldn't go through all this trouble, I'd just do what you're doing - use a tab control and a few flow panels. Or better yet I'd use an off-the-shelf wizard control as are available in many Winforms libraries now, like the DX library.