我定义了一个自定义按钮类,该按钮类在启用/禁用按钮时设置背景色。
I defined a custom button class, which sets background color when button is enabled/disabled.
在运行时启用外观(A):
Enabled appearance at runtime (A):
在运行时(B)禁用外观:
Disabled appearance at runtime (B):
无论 Enabled 的值如何,设计时间外观始终为(A)属性。
Design time appearance is always (A), regardless of the value of Enabled property.
我希望控件在设计器中的显示与运行时的显示完全相同。
I would like my control to appear in designer exactly the way it would appear at run time. Is it possible and, if so, how can do it?
这是我尝试过的内容(仅代码的相关部分):
Here is what I tried (only relevant parts of the code):
Public Class StyledButton : Inherits Button Private p_fEnabled As Boolean <DefaultValue(True)> Public Shadows Property Enabled As Boolean Get Return p_fEnabled End Get Set(value As Boolean) p_fEnabled = value MyBase.Enabled = value UpdateVisualStyle() End Set End Property Private Sub UpdateVisualStyle() If Me.Enabled Then 'set enabled appearance Else 'set disabled appearance End If End Sub End Class推荐答案
shadowed属性确实可以在运行时设计,而不是在IDE中。您不想丢失 Visible = False 的控件,并且即使 Enabled = False 。由于IDE无意绘制禁用的控件,因此没有理由在更改属性时调用Invalidate。
The shadowed property does work as designed at runtime, just not in the IDE. You would not want to loose controls which are Visible = False, and you would want to drill into Button events even when Enabled = False. Since the IDE has no intention of drawing a disabled control, there is no reason for it to invoke Invalidate when you change the property.
由于它在运行时有效,请在设计器中欺骗它以使用另一个看起来像原始属性的属性:
Since it works at runtime, trick it in the designer to use another property which looks like the original:
<Browsable(False), DebuggerBrowsable(DebuggerBrowsableState.Never), EditorBrowsable(False)> Public Shadows Property Enabled As Boolean Get Return neoEnabled End Get Set(value As Boolean) neoEnabled = value End Set End Property一个新属性,为IDE命名正确。
A new property, with the right name for the IDE.
<DisplayName("Enabled")> Public Property neoEnabled As Boolean Get Return p_fEnabled End Get Set(value As Boolean) p_fEnabled = value UpdateVisualStyle() MyBase.Enabled = p_fEnabled End Set End Property遗憾的是, Enabled 和 neoEnabled 都将由Intellisense在代码中提供,但由于他们俩都做同一件事,这没什么大不了的。测试代码:
Sadly, both Enabled and neoEnabled will be offered by Intellisense in code, but since they both do the same thing, its not a big deal. test code:
Private Sub UpdateVisualStyle() If p_fEnabled Then ' other interesting stuff MyBase.BackColor = Color.Lime Else MyBase.BackColor = Color.LightGray End If MyBase.Invalidate() End Sub您可能比我更努力,并提出了一个更干净的实现。
You have probably wrestled with it more that I, and come up with a cleaner implementation.
这将保留与 neoEnabled 状态相关的BackColor:
This persists the BackColor associated with neoEnabled state:
' 'StyledButton1 ' Me.StyledButton1.BackColor = System.Drawing.Color.LightGray Me.StyledButton1.Enabled = False Me.StyledButton1.neoEnabled = False对
Me.StyledButton1.BackColor = System.Drawing.Color.Lime Me.StyledButton1.Enabled = False Me.StyledButton1.neoEnabled = True更多推荐
在设计时控件的已启用外观?
发布评论