自定义边框在WPF标签 [英] Custom Border in a WPF Label

查看:100
本文介绍了自定义边框在WPF标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立一个自定义的WPF控件这样的事情

I need to build a custom WPF Control something like this

由于我是新WPF中,我用下面的code(对不起,VB.NET)

As I am new in WPF, I used the following code (sorry for 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)为什么我的角落都动了一个像素(不是很合适链接)?

1) Is it the best way to do it, considering that I will inherit that control and need the same border on the inherited controls
2) Is it good to specify the default value for the BorderBrush (to be non-transparent), like I did?
3) Why my corners are moved with a pixel (not really right linked)?

推荐答案

一个更好的事情是不是使用了边框类/控制创建自己的装饰类(这基本上是什么边框是)。

A better thing to do is instead of using the Border class/control create your own Decorator class (that's essentially what a Border is).

这篇关于自定义边框在WPF标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆