2012-03-07 98 views
1

我需要建立一个自定义WPF控件像这样 enter image description here自定义边框的WPF标签

正如我在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)相同的边框是好到指定博尔的默认值derBrush(不透明),像我一样?
3)为什么我的角落移动像素(不是很正确的链接)?

回答

0

我在回答你的问题的尝试:

更新: 解答您的评论:

一)建立边境只有在转弯时,你可以尝试使用带有不透明蒙一个简单的边框可见(没有测试尚未虽然)

b)我想你使用的方法是可以的(但是,如果你制作了模板控件,这不会是一个问题;))。

c)对不起,我的错。您可以尝试设置StartLineCapEndLineCapPenLineCap.RoundPenLineCap.Square的值。更多信息可在MSDN上找到:http://msdn.microsoft.com/en-us/library/system.windows.media.pen.aspx

+0

卢卡斯,谢谢你的回答。 a)我不知道如何设置边界,但角落可见; b) - ; c)据我所知,LineJoin用于poligonal线,但我画了8条独立线...... * – serhio 2012-03-08 08:43:39

+0

没问题,我希望我的回答对你有帮助。我已更新它包含aswers评论。 – 2012-03-08 20:03:05

2

一个更好的事情是不是使用Border类/控制的创建自己的Decorator类(即本质上是一个Border是什么)。

+0

这个控件的**继承**怎么样?边界和装饰器不会被继承...而且,我不知道如何设置边框,但角落是可见的; – serhio 2012-03-08 08:39:11

+0

如果您查看Border.cs的源代码,那么您会看到它覆盖了'ArrangeOverride','MeasureOverride'和'OnRender'方法,这些方法是创建您自己的'Decorator'类型的关键。 – 2012-03-08 15:10:43