如何将文本添加到自定义控件? [英] How to add text to a custom control?
问题描述
我正在尝试制作我的第一个自定义控件。我想要做的就是在进度条上的文本中显示一个值。我添加了我需要的属性(TextValue& TextForeColor),但是如何在进度条上显示实际文本?这就是我到目前为止:
Imports System.ComponentModel
Public Class MeterBar
继承ProgressBar
公共属性TextValue()As String
获取
返回MyBase.Text
结束获取
设置(ByVal值为字符串)
MyBase.Text = Value
结束集
结束属性
公共属性TextForeColor()As Color
获取
返回MyBase.ForeColor
结束获取
设置(ByVal值为颜色)
MyBase.ForeColor =价值
结束套件
结束物业
- 我可以显示文字w / o从文本框继承?另外,如何在进度条中设置文本的对齐方式(左/中/右)?请帮忙。
Hi, I am trying to make my first custom control. All I want to do is display a value in Text on a progressbar. I''ve added the properties that I need (TextValue & TextForeColor), but how do I display the actual text on the progressbar? This is what I have so far:
Imports System.ComponentModel
Public Class MeterBar
Inherits ProgressBar
Public Property TextValue() As String
Get
Return MyBase.Text
End Get
Set(ByVal Value As String)
MyBase.Text = Value
End Set
End Property
Public Property TextForeColor() As Color
Get
Return MyBase.ForeColor
End Get
Set(ByVal Value As Color)
MyBase.ForeColor = Value
End Set
End Property
-Can I display text w/o inheriting from textbox? Also, how could I set the alignment of the text w/in the progressbar (Left/Center/Right)? Please help.
推荐答案
简短回答:
(1)您可以使用Label而不是文本框。
(2)要设置对齐方式,可以使用网格环绕用于显示文本的对象。
注意:当你想创建自己的对象时自定义控件,我建议你首先尝试使用现有控件的组合。由于Silverlight(也是WPF)的复杂结构,自己进行控制会花费大量时间和精力。
Short Answers:
(1) You could use Label, instead of textbox.
(2) To set the alignment, you can use grid wrap around the your object that use to display the text.
Note: When you want to create your own custom control, I would recommend you to try to make it with the combination of existing controls first. Making your own control takes a lot of time and difficult because of the complex structure of Silverlight (also WPF).
这篇关于如何将文本添加到自定义控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!