Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF C“控件未在内部渲染”;方框“;_C#_Wpf_Controls_Pie Chart - Fatal编程技术网

C# WPF C“控件未在内部渲染”;方框“;

C# WPF C“控件未在内部渲染”;方框“;,c#,wpf,controls,pie-chart,C#,Wpf,Controls,Pie Chart,由于某些原因,饼图菜单不会在“框”内呈现。很抱歉,我不知道“盒子”的术语是什么(见图)。所以我很难用谷歌搜索解决方案,因为我不知道术语是什么,我得到的结果完全不相关 因此,当我想移动/缩放饼图菜单时,它会扭曲(如图所示,它的宽度) 发疯了)。正如您所看到的,#2应该是元素的正常方式,但在#1中,整个饼图菜单从“框”中消失了。 如何限制饼图切片在“框”中渲染?我从这里获得的这个控件,我所做的只是添加自定义属性 代码(不是我的,它属于作者,我只是添加了一些属性) 度量值覆盖只是返回它得到的可用大

由于某些原因,饼图菜单不会在“框”内呈现。很抱歉,我不知道“盒子”的术语是什么(见图)。所以我很难用谷歌搜索解决方案,因为我不知道术语是什么,我得到的结果完全不相关

因此,当我想移动/缩放饼图菜单时,它会扭曲(如图所示,它的宽度) 发疯了)。正如您所看到的,#2应该是元素的正常方式,但在#1中,整个饼图菜单从“框”中消失了。

如何限制饼图切片在“框”中渲染?我从这里获得的这个控件,我所做的只是添加自定义属性

代码(不是我的,它属于作者,我只是添加了一些属性)


度量值覆盖只是返回它得到的可用大小。不应该是这样的。它应该返回控件及其子控件所需的大小。有关WPF中的双通道布局系统,请参见此

在您的情况下,控件应该找到饼图的直径,并将其作为大小约束返回。

使用Math.Min(renderize.Width/2,renderize.Height/2)


问题原因总结:粗心的错误。

在我返回馅饼的直径后,结果似乎是一样的。馅饼片仍然在“盒子”的外面。您认为我应该通过为面板创建新的Item类将它们视为面板的子级吗?每个切片都应该是Pie菜单的默认项容器(将Pie菜单视为ItemsControl)饼图菜单实际上是一个面板:/n这是否意味着每个切片都应该自己进行渲染,而不是在饼图菜单中对所有切片进行渲染?是的。将饼图设为项目控件,并添加切片作为其项目。然后将此面板设为该饼图控件的ItemsPanel。
    protected override Size MeasureOverride(Size availablesize)
    {
        foreach (UIElement element in Children)
        {
            element.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
        }

        return availablesize;
    }

    protected override Size ArrangeOverride(Size finalsize)
    {
        double radx = this.DesiredSize.Width / 2.0;
        double rady = this.DesiredSize.Height / 2.0;
        Point center = new Point(radx, rady);

        double angle = 0.0, anglestep = 0.0;

        if (this.Children.Count != 0.0)
            anglestep = TotalAngle / (double)this.Children.Count;

        double deg2rad = Math.PI / 180.0;

        foreach (UIElement uie in Children)
        {
            double a = (angle + anglestep / 2.0) * deg2rad;

            uie.Arrange(new Rect(Point.Add(center, new Vector((radx + (double)base.GetValue(CircularPieMenu.ClippingRadiusProperty)) * Math.Cos(a) / 2.0 - uie.DesiredSize.Width / 2.0, 
                                                                (rady + (double)base.GetValue(CircularPieMenu.ClippingRadiusProperty)) * Math.Sin(a) / 2.0 - uie.DesiredSize.Height / 2.0)), 
                                           uie.DesiredSize));

            angle += anglestep;
        }

        return finalsize;
    }

    protected override void OnRender(DrawingContext dc)
    {
        double radx = this.DesiredSize.Width / 2.0;
        double rady = this.DesiredSize.Height / 2.0;
        Size radiusSize = new Size(radx, rady);
        Point center = new Point(radx, rady);

        double currentAngle = 0.0;
        double angleStep = 0.0;

        if (this.Children.Count != 0.0)
        {
            angleStep = TotalAngle / (double)this.Children.Count;
        }

        double deg2rad = Math.PI / 180.0;

        // this is the code where caused the "hole" in the center of the "ellipse"
        EllipseGeometry entirePart = new EllipseGeometry(center, radx + 1, rady + 1);
        EllipseGeometry excludedPart = new EllipseGeometry(center, ClippingRadius, ClippingRadius);
        dc.PushClip(new CombinedGeometry(GeometryCombineMode.Exclude, entirePart, excludedPart));

        // this produce a full ellipse (not like arc segment like u would think)
        // we want to have arcs, instead of a full ellipse :D
        dc.DrawEllipse(BackgroundColor, new Pen(OuterCircumferenceColor, OuterCircumferenceThickness), center, radx, rady);

        if (ClippingRadius > 0.0)
        {
            dc.DrawEllipse(null, new Pen(InnerCircumferenceColor, 1.0), center, ClippingRadius + 1, ClippingRadius + 1);
        }

        double startAngle = 0.0, endAngle = 0.0;

        foreach (UIElement element in this.Children)
        {
            double angle = currentAngle * deg2rad;

            // line between segments
            dc.DrawLine(new Pen(LineColor, LineThickness), center, Point.Add(center, new Vector(radx * Math.Cos(angle), rady * Math.Sin(angle))));

            if (this.IsMouseOver && element.IsMouseOver)
            {
                startAngle = angle;
                endAngle = startAngle + angleStep * deg2rad;
            }

            currentAngle += angleStep;
        }

        // only related when mouse down
        if (this.IsMouseOver)
        {
            PathGeometry path = new PathGeometry();
            PathFigure pathfig = new PathFigure();
            pathfig.StartPoint = center;
            pathfig.Segments.Add(new LineSegment(Point.Add(center, new Vector(radx * Math.Cos(startAngle), rady * Math.Sin(startAngle))), true));
            pathfig.Segments.Add(new ArcSegment(Point.Add(center, new Vector(radx * Math.Cos(endAngle), rady * Math.Sin(endAngle))), new Size(1.0, 1.0), 0.0, false, SweepDirection.Clockwise, true));
            pathfig.Segments.Add(new LineSegment(center, true));
            path.Figures.Add(pathfig);
            dc.PushClip(path);
            currentAngle = 0;

            if (this.ismouseleftdown)
            {
                dc.DrawEllipse(ForegroundColor, new Pen(new SolidColorBrush(Color.FromRgb(171, 161, 140)), 1.0), center, radx, rady);
                dc.DrawEllipse(null, new Pen(new LinearGradientBrush(Color.FromRgb(223, 183, 136), Colors.Transparent, 45.0), 1.0), center, radx - 1, rady - 1);

                if ((double)base.GetValue(CircularPieMenu.ClippingRadiusProperty) > 0.0)
                    dc.DrawEllipse(null, new Pen(new SolidColorBrush(Color.FromRgb(171, 161, 140)), 1.0), center, (double)base.GetValue(CircularPieMenu.ClippingRadiusProperty) + 1, (double)base.GetValue(CircularPieMenu.ClippingRadiusProperty) + 1);

                foreach (UIElement uie in this.Children)
                {
                    double a = currentAngle * deg2rad;
                    dc.DrawLine(new Pen(new SolidColorBrush(Color.FromRgb(171, 161, 140)), 1.0), center, Point.Add(center, new Vector(radx * Math.Cos(a), rady * Math.Sin(a))));

                    currentAngle += angleStep;
                }
            }
            else
            {
                // chg to mouse over color                                       circumference color
                dc.DrawEllipse(ForegroundColor, new Pen(new SolidColorBrush(Color.FromRgb(210, 192, 141)), 1.0), center, radx, rady);
                //dc.DrawEllipse(null, new Pen(new LinearGradientBrush(Color.FromRgb(255, 255, 247), Colors.Transparent, 45.0), 1.0), center, radx - 1, rady - 1);

                if ((double)base.GetValue(CircularPieMenu.ClippingRadiusProperty) > 0.0)
                    dc.DrawEllipse(null, new Pen(new SolidColorBrush(Color.FromRgb(210, 192, 141)), 1.0), center, (double)base.GetValue(CircularPieMenu.ClippingRadiusProperty) + 1, (double)base.GetValue(CircularPieMenu.ClippingRadiusProperty) + 1);

                foreach (UIElement uie in this.Children)
                {
                    double a = currentAngle * deg2rad;
                    dc.DrawLine(new Pen(new SolidColorBrush(Color.FromRgb(210, 192, 141)), 1.0), center, Point.Add(center, new Vector(radx * Math.Cos(a), rady * Math.Sin(a))));

                    currentAngle += angleStep;
                }
            }

            dc.Pop();
        }

        dc.Pop();
    }