如何从代码隐藏中绘制复杂形状以用于资源字典中的自定义控件
我是 wpf 的新手,遇到了一个可能很微不足道的问题。我在资源字典中定义了一个自定义控件,如下所示:
<ResourceDictionary
x:Class="SyringeSlider.Themes.Generic"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SyringeSlider">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Canvas Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Name="syringeCanvas">
</Canvas>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
不幸的是,我无法超越这一点,因为我想在画布上绘制一个几何图形,该几何图形由一组多线几何图形组成,其尺寸根据可用空间的函数计算画布。我相信我需要一个代码隐藏方法来执行此操作,但无法确定如何将 xaml 定义链接到代码隐藏方法。
请注意,我专门为此目的设置了一个类 x:Class="SyringeSlider.Themes.Generic",但无法确定将绘图方法链接到哪个 Canvas 属性。
我的绘图方法看起来像这样
private void CalculateSyringe()
{
int adjHeight = (int) Height - 1;
int adjWidth = (int) Width - 1;
// Calculate some very useful values based on the chart above.
int borderOffset = (int)Math.Floor(m_borderWidth / 2.0f);
int flangeLength = (int)(adjHeight * .05f);
int barrelLeftCol = (int)(adjWidth * .10f);
int barrelLength = (int)(adjHeight * .80);
int barrelRightCol = adjWidth - barrelLeftCol;
int coneLength = (int)(adjHeight * .10);
int tipLeftCol = (int)(adjWidth * .45);
int tipRightCol = adjWidth - tipLeftCol;
int tipBotCol = adjWidth - borderOffset;
Path mySyringePath = new Path();
PathGeometry mySyringeGeometry = new PathGeometry();
PathFigure mySyringeFigure = new PathFigure();
mySyringeFigure.StartPoint = new Point(0, 0);
Point pointA = new Point(0, flangeLength);
mySyringeFigure.Segments.Add(new LineSegment(pointA, true));
Point pointB = new Point();
pointB.Y = pointA.Y + barrelLength;
pointB.X = 0;
mySyringeFigure.Segments.Add(new LineSegment(pointB, true));
// You get the idea....Add more points in this way
mySyringeGeometry.Figures.Add(mySyringeFigure);
mySyringePath.Data = mySyringeGeometry;
}
,所以我的问题是:
1)我想做的事情有意义吗? 2)可以使用画布来实现此目的吗?如果没有,我还有什么其他选择?
谢谢!
I am new to wpf and am having a problem which may or may not be trivial. I have defined a custom control as follows in the resource dictionary:
<ResourceDictionary
x:Class="SyringeSlider.Themes.Generic"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SyringeSlider">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Canvas Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Name="syringeCanvas">
</Canvas>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Unfortunately I cannot go beyond this because I would like to draw a geometry onto the canvas consisting of a set of multiple line geometries whose dimensions are calculated as a function of the space available in the canvas. I believe that I need a code behind method to do this, but have not been able to determine how to link the xaml definition to a code behind method.
Note that I have set up a class x:Class="SyringeSlider.Themes.Generic" for specifically this purpose, but can't figure out which Canvas property to link the drawing method to.
My drawing method looks like this
private void CalculateSyringe()
{
int adjHeight = (int) Height - 1;
int adjWidth = (int) Width - 1;
// Calculate some very useful values based on the chart above.
int borderOffset = (int)Math.Floor(m_borderWidth / 2.0f);
int flangeLength = (int)(adjHeight * .05f);
int barrelLeftCol = (int)(adjWidth * .10f);
int barrelLength = (int)(adjHeight * .80);
int barrelRightCol = adjWidth - barrelLeftCol;
int coneLength = (int)(adjHeight * .10);
int tipLeftCol = (int)(adjWidth * .45);
int tipRightCol = adjWidth - tipLeftCol;
int tipBotCol = adjWidth - borderOffset;
Path mySyringePath = new Path();
PathGeometry mySyringeGeometry = new PathGeometry();
PathFigure mySyringeFigure = new PathFigure();
mySyringeFigure.StartPoint = new Point(0, 0);
Point pointA = new Point(0, flangeLength);
mySyringeFigure.Segments.Add(new LineSegment(pointA, true));
Point pointB = new Point();
pointB.Y = pointA.Y + barrelLength;
pointB.X = 0;
mySyringeFigure.Segments.Add(new LineSegment(pointB, true));
// You get the idea....Add more points in this way
mySyringeGeometry.Figures.Add(mySyringeFigure);
mySyringePath.Data = mySyringeGeometry;
}
SO my question is:
1) Does what I am trying to do make any sense?
2) Can a use a canvas for this purpose? If not, what are my other options?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您要创建自定义控件并创建模板,因此请重写控件代码中的 OnApplyTemplate() 函数。您必须在那里搜索模板部分,然后在代码中获取引用。
像这样的东西。
不要忘记在 xaml 中命名您的画布。
Since you are creating a custom control and creating a template, override the OnApplyTemplate() function in your control's code. There you have to search for template parts and then you get the references in your code.
Something like this.
Don't forget to name your canvas in the xaml.