参见上图。这是Visual Studio选项表单的屏幕截图。
左侧本质上是TreeView。右侧是用于更改程序选项的各种控件。在TreeView中选择节点时,右侧会发生变化,显示不同的选项。
你如何编程这样的东西?右侧是否只有50个重叠的面板,并且节点的选择只是更改了哪个面板可见?如果是这样,您将如何进行管理?在设计器中这将是一团糟。
不,您不会制作50个重叠的面板。只需创建几个用户控件,例如,在节点的标签上链接类型。您可以使用激活器来创建控件。创建1个树状视图和1个面板:(伪代码)
// create nodes:
TreeNode item = new TreeNode();
item.Tag = typeof(UserControl1);
TreeView.Nodes.Add( item );
// field currentControl
UserControl _currentControl;
// on selection:
TreeViewItem item = (TreeViewItem)sender;
if(_currentControl != null)
{
_currentControl.Controls.Remove(_currentControl);
_currentControl.Dispose();
}
// if no type is bound to the node, just leave the panel empty
if (item.Tag == null)
return;
_currentControl = (UserControl)Activator.Create((Type)item.Tag);
Panel1.Controls.Add(_currentControl);
下一个问题是“我想在控件中调用保存方法或RequestClose方法”。为此,您应该在控件上实现一个接口,并且在切换节点时,只需尝试将_currentusercontrol强制转换为IRequestClose接口并调用,例如,bool RequestClose();。方法。
// on selection:
TreeViewItem item = (TreeViewItem)sender;
if(_currentControl != null)
{
// if the _currentControl supports the IRequestClose interface:
if(_currentControl is IRequestClose)
// cast the _currentControl to IRequestCode and call the RequestClose method.
if(!((IRequestClose)_currentControl).RequestClose())
// now the usercontrol decides whether the control is closed/disposed or not.
return;
_currentControl.Controls.Remove(_currentControl);
_currentControl.Dispose();
}
if (item.Tag == null)
return;
_currentControl = (UserControl)Activator.Create(item.Tag);
Panel1.Controls.Add(_currentControl);
但这将是下一步。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句