在WinRT中绘制二叉树

本文关键字:二叉树 绘制 WinRT | 更新日期: 2023-09-27 17:51:05

基本上,我想在WinRT中显示二叉树。我有一个包含节点值的ObservableCollection

你认为开始尝试的最好方法是什么?

在WinRT中绘制二叉树

如果一个"标准的" TreeView控件不能满足你的审美需求-你可以尝试用一些Shape元素构建一个树,可能连接并最有可能在Canvas上布局,或者使用DirectX -使用本地WinRT组件或使用SharpDX。

您可以使用自己的面板(自定义)来实现您的需求,

   public class MyBinaryTreePanel : Panel
    {
        public double MaxRowHeight { get; set; }
        public MyBinaryTreePanel()
        {
            MaxRowHeight = 0.0;
        }
        protected override Size ArrangeOverride(Size finalSize)
        {            
            double rowHeight=0;
            double columnWidth = finalSize.Width;
            int total = Children.Count;
            int temp = total;
            int count = 0;
            do
            {
                temp /= 2;
                count++;
            } while (temp != 1);
            count++;           
            int Row = count;   
            MaxRowHeight = finalSize.Height / total;
            double temrow = 0;
            int i = 0;
            for (int a = 0; a < Row; a++)
            {
                double temp34 = 0;
                double tempColumn = 0;                
                columnWidth = ((finalSize.Width) / (Math.Pow(2, a)));
                for (int b = 0; b < (Math.Pow(2, a)); b++)
                {
                    if (i < total)
                    {
                        rowHeight = Children[i].DesiredSize.Height > MaxRowHeight ? Children[i].DesiredSize.Height : MaxRowHeight;
                        Children[i].Arrange(new Rect( tempColumn,  temrow, columnWidth, rowHeight));
                        i++;
                        if (rowHeight >= temp34)
                        {
                            temp34 = rowHeight;
                        }
                        else
                        {
                            rowHeight = temp34;
                        }
                        tempColumn += columnWidth;
                    }
                }
                temrow += temp34;
            }//
            return finalSize;
        }
        protected override Size MeasureOverride(Size availableSize)
        {
            int total = Children.Count;
            int temp = total;
            int count = 0;
            do
            {
                temp /= 2;
                count++;
            } while (temp != 1);
            count++;
            int Row = count;
            MaxRowHeight = (availableSize.Height) / Row;
            Size MyDesiredSize = new Size();
            int i = 0;
            for (int a = 0; a < Row; a++)
            {
                double value2 = 0.0;
                for (int b = 0; b < (Math.Pow(2, a)); b++)
                {
                    if (i < total)
                    {
                        Children[i].Measure(availableSize);
                        double value1 = Children[i].DesiredSize.Height;
                        if (value1 >= value2)
                        {
                            MyDesiredSize.Height = value1;
                            value2 = value1;
                        }
                        else
                        {
                            MyDesiredSize.Height = value2;
                        }
                        i++;
                    }
                }
                MyDesiredSize.Height = MyDesiredSize.Height > MaxRowHeight ? MyDesiredSize.Height : MaxRowHeight;
            }
                           return MyDesiredSize;
        }
    }

我希望它能帮助你,

问候,快乐雷克斯

您可以使用WinRT Xaml Toolkit中的TreeView控件。

下面是示例用法:https://winrtxamltoolkit.codeplex.com/SourceControl/latest#WinRTXamlToolkit.Sample/Views/Controls/TreeViewTestPage.xaml

控件是从Silverlight Toolkit中移植过来的,带有原始的以及一个更友好的触摸模板。您可以在NuGet上找到WinRTXamlToolkit。