【问题标题】:Drawing a binary tree in WinRT在 WinRT 中绘制二叉树
【发布时间】:2014-03-20 17:14:42
【问题描述】:

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

您认为开始尝试的最佳方式是什么?

【问题讨论】:

  • 您应该能够在 WinRT 中重用大部分 this,但您可能需要更改一些 XAML,因为 WinRT 中没有几个 XAML 功能。

标签: c# windows-runtime treeview winrt-xaml winrt-xaml-toolkit


【解决方案1】:

如果“标准”TreeView 控件不能满足您的审美需求 - 您可以尝试用一些 Shape 元素构建一棵树,这些元素可能已连接并且很可能布置在 Canvas 上,或者支持巨大的树 - 使用 DirectX - 使用本机 WinRT 组件或使用 SharpDX。

【讨论】:

    【解决方案2】:

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

       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;
            }
        }
    

    希望对你有帮助

    问候, 乔伊雷克斯

    【讨论】:

      【解决方案3】:

      您可以使用 WinRT Xaml 工具包中的 TreeView 控件。

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

      该控件是从 Silverlight Toolkit 移植的,带有原始模板以及更易于触摸的模板。你可以找到WinRTXamlToolkit on NuGet

      【讨论】:

        猜你喜欢
        • 2019-05-04
        • 2016-02-24
        • 2012-11-22
        • 2017-10-09
        • 1970-01-01
        • 2014-12-23
        • 2019-08-28
        • 1970-01-01
        • 2015-08-20
        相关资源
        最近更新 更多