首页 文章

WPF标签中的自定义边框[重复]

提问于
浏览
1

这个问题在这里已有答案:

我需要构建一个像这样的自定义WPF控件
enter image description here

由于我是WPF的新手,我使用了以下代码(对不起VB.NET)

Public Class TextPlaceholder
  Inherits System.Windows.Controls.Label

  Const CustomBorderWidth As Integer = 2

  Public Sub New()
    MyBase.New()
    Me.BorderBrush = SystemColors.ActiveBorderBrush
  End Sub

  Protected Overrides Sub OnRender(drawingContext As System.Windows.Media.DrawingContext)
    MyBase.OnRender(drawingContext)

    Dim pointTopLeft As New Point(-1, -1)
    Dim pointTopRight As New Point(Me.ActualWidth, -1)
    Dim pointBottomLeft As New Point(-1, Me.ActualHeight)
    Dim pointBottomRight As New Point(Me.ActualWidth, Me.ActualHeight)

    Dim myPen As New Pen(Me.BorderBrush, CustomBorderWidth)
    drawingContext.DrawLine(myPen, pointTopLeft, New Point(pointTopLeft.X + 5, pointTopLeft.Y))
    drawingContext.DrawLine(myPen, pointTopLeft, New Point(pointTopLeft.X, pointTopLeft.Y + 5))

    drawingContext.DrawLine(myPen, pointTopRight, New Point(pointTopRight.X - 5, pointTopRight.Y))
    drawingContext.DrawLine(myPen, pointTopRight, New Point(pointTopRight.X, pointTopRight.Y + 5))

    drawingContext.DrawLine(myPen, pointBottomLeft, New Point(pointBottomLeft.X + 5, pointBottomLeft.Y))
    drawingContext.DrawLine(myPen, pointBottomLeft, New Point(pointBottomLeft.X, pointBottomLeft.Y - 5))

    drawingContext.DrawLine(myPen, pointBottomRight, New Point(pointBottomRight.X - 5, pointBottomRight.Y))
    drawingContext.DrawLine(myPen, pointBottomRight, New Point(pointBottomRight.X, pointBottomRight.Y - 5))
  End Sub

End Class

现在

1)这是最好的方法,考虑到我将继承该控件并在继承的控件上需要相同的边框
2)像我一样指定BorderBrush的默认值(不透明)是否合适?
3)为什么我的角落被移动了一个像素(没有真正正确的链接)?

2 回答

  • 0

    更好的办法是使用 Border class / control创建自己的 Decorator 类(这实质上是 Border ) .

  • 2

    我试图回答你的问题:

    Update: 您的评论的回答:

    a)创建仅在角落可见的边框,你可以尝试使用带有不透明蒙版的简单边框(尽管还没有测试过)

    b)我认为你使用的方法在你的情况下是可以的(但是,如果你做了一个模板控制,这不会是一个问题;)) .

    c)抱歉,我的错误 . 您可以尝试将 StartLineCapEndLineCap 设置为 PenLineCap.RoundPenLineCap.Square 值 . 更多信息可以在MSDN上找到:http://msdn.microsoft.com/en-us/library/system.windows.media.pen.aspx

相关问题