带文本框的连接线,绑定问题

发布于 2024-12-01 07:22:50 字数 223 浏览 0 评论 0原文

我想画一条线,当我双击它时,我想在上面放置一个文本。我正在考虑将其放入内容控件中并画一条线,在顶部放置一个折叠的文本框,检测双击,显示文本框将其设置为文本块等。我遇到的唯一问题是我不知道该怎么办设置线条的坐标,因为它位于内容控件内部,因此会绘制实际的线条。我已经被困了几个小时,任何帮助将不胜感激。

基本上我需要一个具有起点和终点属性的对象,该对象具有线条的形状和内容呈现器。但我不知道该怎么做。任何指示将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裸钻 2024-12-08 07:22:50

我们也用同样的方法来标记我们的联系。如果您通过路径绘制连接,则可以使用

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

这种方式,您将在几何图形上拥有中心位置。现在您可以将其存储到用于定位标签的依赖属性中。当然,每次形状/几何形状改变其位置或大小时都必须调用它。

结合此控件的一个小示例。

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;
    }
}

您的 ControlTemplate 看起来像这样

<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/>

现在要添加 ContentControl 功能,您可以添加 ContentPresenter 来代替 TextBox。

基本上我需要一个具有起点和终点属性的对象
具有线的形状

为此,只需为您的 2 个位置添加 2 个 dp 属性。确保添加依赖属性更改处理程序以调用 UpdatePath 方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文