开发者

How to draw complex shape from code behind for custom control in resource dictionary

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"
    x开发者_StackOverflow中文版mlns: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!


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.

public class MyControl : Control
{
    private Canvas myCanvas;

    public override void OnApplyTemplate()
    {
        Canvas theCanvas = Template.FindName("syringeCanvas", this) as Canvas;

        if(theCanvas != null)
        {
            //<-- Save a reference to the canvas
            myCanvas = theCanvas;

            //<-- Do some stuff.
        }
    }
}

Don't forget to name your canvas in the xaml.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜