答案 0 :(得分:1)
作为一种选择,您可以依靠ElementHost
Windows窗体控件在其中托管WPF TreeView
控件。然后,对于WPF TreeView
及其节点,将RotateTransform
设置为180度。
示例
在下面的示例中,我创建了一个包含UserControl
的Windows窗体ElementHost
来承载WPF TreeView
控件,并为树设置了180度RotateTransform
及其节点,我正在制作一棵倒置(自底向上)的树:
这是代码。只需将代码作为类添加到您的应用程序中,并确保您已经添加了所需的程序集,这些程序集在本文结尾处提到。然后构建项目并将MyTreeView
的实例放在表单上。
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Media;
public class MyTreeView : System.Windows.Forms.UserControl, ISupportInitialize
{
private ElementHost elementHost = new ElementHost();
private TreeView tree;
private System.Windows.Forms.TreeView winFormsTree;
public MyTreeView()
{
tree = new TreeView();
winFormsTree = new System.Windows.Forms.TreeView();
Nodes = winFormsTree.Nodes;
tree.LayoutTransform = new RotateTransform(180);
tree.FlowDirection = FlowDirection.RightToLeft;
elementHost.Dock = System.Windows.Forms.DockStyle.Fill;
elementHost.Name = "elementHost";
elementHost.Child = tree;
Controls.Add(elementHost);
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public System.Windows.Forms.TreeNodeCollection Nodes { get; }
public void BeginInit() { }
public void RefreshTree()
{
tree.Items.Clear();
RefreshTree(tree.Items, Nodes);
}
private void RefreshTree(ItemCollection items,
System.Windows.Forms.TreeNodeCollection nodes)
{
foreach (System.Windows.Forms.TreeNode node in nodes)
{
var item = new TreeViewItem();
var label = new Label();
label.LayoutTransform = new RotateTransform(180);
label.Content = node.Text;
label.Padding = new Thickness(0);
item.Header = label;
items.Add(item);
RefreshTree(item.Items, node.Nodes);
}
}
public void EndInit()
{
RefreshTree();
}
protected override void Dispose(bool disposing)
{
if (disposing && winFormsTree != null)
winFormsTree.Dispose();
base.Dispose(disposing);
}
}
参考程序集
以下是必需的引用程序集:PresentationCore
,PresentationFramework
,WindowsBase
,WindowsFormsIntegration
。
注意
在此示例中,我使用了TreeNodeCollection
。您可以使用任何其他喜欢的结构。您可以使用Nodes
属性以设计方式添加节点,并在运行时查看树。作为此示例的一个已知问题,您在Nodes
属性中所做的更改不会在设计时立即反映出来,而是被序列化并在运行时显示。