我是 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)可以为此目的使用画布吗?如果没有,我的其他选择是什么?
谢谢!