开发者

Connector Lines with textbox, binding issue

开发者 https://www.devze.com 2023-03-31 06:07 出处:网络
I want to draw a line and when I double click it I want to put a text on it. I was thinking of putting it in a contentcontrol and drawing a line, put a collapsed textbox on top, detect a double click,

I want to draw a line and when I double click it I want to put a text on it. I was thinking of putting it in a contentcontrol and drawing a line, put a collapsed textbox on top, detect a double click, show the textbox set it to a textblock, etc. The only problem I run into I don't know what to set the coordinates of the line since it is inside the contentcontrol, so an actual line gets drawn. i've been stuck for hours, any help would be appreciated.

Basically I need an object with开发者_开发百科 a start and end point properties which has the shape of a line , with a content presenter. But I don't know how to go about doing this. Any pointers would be appreciated.


We do the same for labeling our connections. If you draw your connection via a path you can use

LineGeometry.GetPointAtFractionLength(0.5, out midPoint, out tangetMidPoint);

That way you would have the center position on your geometry. Now you could store this into a dependency property which you use to position the label. Of course this must be called everytime your shape/geometry changes its position or size.

a small example for a control combining this.

public class LabeledLine : ContentControl
{
    public static readonly DependencyProperty LabelPosition ...
    public static readonly DependencyProperty LineGeometry ...

    // call me everytime the LineGeometry gets changed.
    public void UpdatePath()
    {

        LineGeometry.GetPointAtFractionLength(0.5, out midPoint, out tangetMidPoint);
        LabelPosition = midPoint;
    }
}

Your ControlTemplate would look something like that

<ControlTemplate TargetType="{x:Type local:LabeledLine}">
    <Canvas x:Name="canvas">
        <Path Data="{TemplateBinding LineGeometry}"/>
        <TextBox Canvas.Left="{TemplateBinding LabelPosition.X}" Canvas.Top="{TemplateBinding LabelPosition.Y}"/>
    </Canvas>
<ControlTemplate/>

Now to add the ContentControl functionality you could add the ContentPresenter in place of the TextBox.

Basically I need an object with a start and end point properties which has the shape of a line

For that just add 2 dp properties for your 2 positions. Make sure to add a dependency property changed handler to call the UpdatePath method.

0

精彩评论

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