Visual Studio 2008 表单设计器搞乱了布尔属性值
我有一个非常烦人的问题,我花了几周时间试图解决。我有一个 WinForms C# 项目,我在其中开发了自定义控件(ListView + ToolStrip 和 ToolStripButtons)。该控件在解决方案内以不同的形式使用 - 但在其他项目中。对于不同的表单,我需要使某些按钮可见或隐藏,因此我在控件中添加了相应的属性,例如
public Boolean DeleteButtonVisible
{
get
{
return tsbDelete.Visible;
}
set
{
tsbDelete.Visible = value;
}
}
某些按钮默认可见,有些按钮默认隐藏。在设计器中,当使用我的控件编辑表单时,我可以更改这些属性,控件上的按钮按其应有的方式变得可见或隐藏。但是每次我以所有形式更改控件源文件中的任何内容时,无论我在设计器中设置了什么,这些属性都会重置为默认值,并且我必须恢复手动这些值。好吧,我使用的是源代码管理,所以这并不难,但是每次我在另一个文件中更改一点时对几十个文件执行“撤消”是一场灾难。
我尝试使用 [DesignerSerializationVisibility] 属性来解决此问题。如果我将它与值“隐藏”一起使用,它根本没有任何好处 - 值只是没有保存。 “内容”使按钮随机消失,即使默认情况下它们是可见的。 “可见”不会产生任何效果,因为这是默认值...
我不想为我的代码中的每个表单设置每个按钮的可见性 - 这不是应该完成的方式。
有人知道这件事吗?
I have a very annoying problem I'm trying to solve for couple of weeks. I have a WinForms C# project where I developed my custom control (ListView + ToolStrip with ToolStripButtons). This control is used in different forms inside solution - but in other projects. For different forms I need to make certain buttons visible or hidden, so I have added to my control corresponding properties like
public Boolean DeleteButtonVisible
{
get
{
return tsbDelete.Visible;
}
set
{
tsbDelete.Visible = value;
}
}
Some buttons are visible by default, some are hidden. In designer when editing a form with my control I'm able to change those properties, buttons on control become visible or hidden as they should. But every time I'm changing anything in my control source file in all forms those properties are reset to default values regardless of what I have set in designer and I have to restore those values manually. Well, I'm using a source control so this is not that hard, but performing "Undo" on a couple dozen of files every time I change a bit in another file is a damn disaster.
I have tried to use [DesignerSerializationVisibility] attribute to fix this issue. If I used it with value "Hidden" it didn't do any good at all - values were just not saved. "Content" made buttons randomly disappear even if by default they were visible. "Visible" lead to no effect, as this is default value...
I don't want to set every button visibility for every form in my code - this is just not the way it should be done.
Does anyone know something about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,Control.Visible 属性很特殊。 getter不返回最后分配的值,它仅在控件实际可见时返回 true。这可能会产生副作用,你已经发现了。在这种情况下,可能会在控制切换出设计模式时引起。要正确执行此操作,您必须将分配的状态存储在支持变量中。像这样:
确保将支持变量的默认值初始化为 tsbDelete.Visible 的默认值。使用构造函数来确定。
Yes, the Control.Visible property is special. The getter does not return the last assigned value, it only returns true when the control is actually visible. That can have side-effects, you've found one. In this case probably induced when the control switches out of design mode. To do this correctly, you must store the assigned state in a backing variable. Like this:
Be sure to initialize the default value of the backing variable to the default value of tsbDelete.Visible. Use the constructor to be sure.