如何预览我的自定义面板? [英] How to preview my custom panel?
本文介绍了如何预览我的自定义面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个自定义面板,并覆盖了测量和排列功能,当它运行时,将孩子安排在正确的位置,但是,我无法在混合设计时预览孩子
类RadialPanel:Panel
{
protected override Size MeasureOverride(Size availableSize)
{
foreach(UIElement elem in Children)
{
elem.Measure(new size(float.PositiveInfinity,float.PositiveInfinity));
}
return base.MeasureOverride(availableSize);
}
protected override Size ArrangeOverride(Size finalSize)
{
if(Children.Count == 0)
{
return finalSize ;
}
double Angle = 0;
double AngularSpace =(360.0 / Children.Count)*(Math.PI / 180);
double radiusX = finalSize.Width / 2.4;
double radiusY = finalSize.Height / 2.4;
foreach(儿童的UIElement元素)
{
Point childPosition = new Point(Math.Cos(Angle)* radiusX,-Math.Sin(Angle) * radiusY);
Point ActualPoint = new Point(finalSize.Width * 0.5 + childPosition.X - elem.DesiredSize.Width * 0.5,finalSize.Height * 0.5 + childPosition.Y - elem.DesiredSize.Height * 0.5) ;
elem.Arrange(new Rect(ActualPoint.X,ActualPoint.Y,elem.DesiredSize.Width,elem.DesiredSize.Height));
Angle + = AngularSpace;
}
返回finalSize;
//返回base.ArrangeOverride(finalSize);
}
}
解决方案
>>但是,我无法在混合设计时预览孩子
我没有测试你的代码,但这里有一个样本来制作 RadialPanel 。
#Code:
public class RadialPanel:Panel
{
//衡量每个孩子并给他们想要的空间
protected override尺寸MeasureOverride(尺寸availableSize)
{
foreach(儿童的UIElement elem)
{
//为所有孩子提供无限大小可用大小
elem.Measure(new Size(double.PositiveInfinity,double.PositiveInfinity));
}
return base.MeasureOverride(availableSize);
}
//根据圆的几何方程排列所有孩子。
protected override Size ArrangeOverride(Size finalSize)
{
if(Children.Count == 0)
return finalSize;
double _angle = 0;
//度数转换为Radian乘以PI / 180
double _incrementalAngularSpace =(360.0 / Children.Count)*(Math.PI / 180);
//基于可用尺寸的近似半径,这里需要更好的方法。
double radiusX = finalSize.Width / 2.4;
double radiusY = finalSize.Height / 2.4;
foreach(UIElement elem in Children)
{
//计算元素圆上的点
Point childPoint = new Point(Math.Cos(_angle) )* radiusX,-Math.Sin(_angle)* radiusY);
//将点偏移到可用的矩形区域,即FinalSize。
Point actualChildPoint = new Point(finalSize.Width / 2 + childPoint.X - elem.DesiredSize.Width / 2,finalSize.Height / 2 + childPoint.Y - elem.DesiredSize.Height / 2);
//通过将计算出的点作为placementPoint,在子元素上调用Arrange方法。
elem.Arrange(new Rect(actualChildPoint.X,actualChildPoint.Y,elem.DesiredSize.Width,elem.DesiredSize.Height));
//计算下一个元素的新_angle
_angle + = _incrementalAngularSpace;
}
返回finalSize;
}
}
#Usage:
最好的问候,
Bob < /强>
Hi,
I create a custom panel, and override Measure and Arrange function, when it running, arrange children at correct position, however, I can not preview the children in blend design time
class RadialPanel : Panel { protected override Size MeasureOverride(Size availableSize) { foreach(UIElement elem in Children) { elem.Measure(new Size(float.PositiveInfinity, float.PositiveInfinity)); } return base.MeasureOverride(availableSize); } protected override Size ArrangeOverride(Size finalSize) { if(Children.Count == 0) { return finalSize; } double Angle = 0; double AngularSpace = (360.0 / Children.Count) * (Math.PI / 180); double radiusX = finalSize.Width / 2.4; double radiusY = finalSize.Height / 2.4; foreach(UIElement elem in Children) { Point childPosition = new Point(Math.Cos(Angle) * radiusX, -Math.Sin(Angle) * radiusY); Point ActualPoint = new Point(finalSize.Width * 0.5 + childPosition.X - elem.DesiredSize.Width * 0.5, finalSize.Height * 0.5 + childPosition.Y - elem.DesiredSize.Height * 0.5); elem.Arrange(new Rect(ActualPoint.X, ActualPoint.Y, elem.DesiredSize.Width, elem.DesiredSize.Height)); Angle += AngularSpace; } return finalSize; //return base.ArrangeOverride(finalSize); } }
解决方案
Hi,
>>however, I can not preview the children in blend design time
I not test your code, but here is a sample to make a RadialPanel .
#Code:
public class RadialPanel : Panel { // Measure each children and give as much room as they want protected override Size MeasureOverride(Size availableSize) { foreach (UIElement elem in Children) { //Give Infinite size as the avaiable size for all the children elem.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); } return base.MeasureOverride(availableSize); } //Arrange all children based on the geometric equations for the circle. protected override Size ArrangeOverride(Size finalSize) { if (Children.Count == 0) return finalSize; double _angle = 0; //Degrees converted to Radian by multiplying with PI/180 double _incrementalAngularSpace = (360.0 / Children.Count) * (Math.PI / 180); //An approximate radii based on the avialable size , obviusly a better approach is needed here. double radiusX = finalSize.Width / 2.4; double radiusY = finalSize.Height / 2.4; foreach (UIElement elem in Children) { //Calculate the point on the circle for the element Point childPoint = new Point(Math.Cos(_angle) * radiusX, -Math.Sin(_angle) * radiusY); //Offsetting the point to the Avalable rectangular area which is FinalSize. Point actualChildPoint = new Point(finalSize.Width / 2 + childPoint.X - elem.DesiredSize.Width / 2, finalSize.Height / 2 + childPoint.Y - elem.DesiredSize.Height / 2); //Call Arrange method on the child element by giving the calculated point as the placementPoint. elem.Arrange(new Rect(actualChildPoint.X, actualChildPoint.Y, elem.DesiredSize.Width, elem.DesiredSize.Height)); //Calculate the new _angle for the next element _angle += _incrementalAngularSpace; } return finalSize; } }
#Usage:
Best Regards,
Bob
这篇关于如何预览我的自定义面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文