Winforms DataBind 到控件的可见属性
将数据绑定到控件的可见属性时是否存在任何已知问题?
无论我的属性是什么,该控件始终不可见。
Public ReadOnly Property IsRibbonCategory() As Boolean
Get
Return True
End Get
End Property
我尝试了控件的文本属性和其他属性,它们似乎工作正常。
我正在尝试设置面板的可见属性。
Are there any known issues when databinding to a control's visible property?
The control is always NOT visible regardless of what my property is.
Public ReadOnly Property IsRibbonCategory() As Boolean
Get
Return True
End Get
End Property
I tried the control's text property and other properties and they seem to work correctly.
I am trying to set a Panel's visible property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我发现,如果您假设与控件的 Visible 属性的绑定已被破坏,那么生活会更好,尽管事实上它有时会起作用。 请参阅 http://support.microsoft.com/kb/327305,其中说了这么多(并且虽然知识库文章适用于 .NET 1.0 和 1.1,但至少在 2.0 中它似乎仍然是一个问题)。
我创建了一个用于创建绑定的实用程序类,除其他外,它为我提供了一个集中的位置来添加解决方法。 它没有实际在 Visible 上创建绑定,而是执行两件事:
这需要一些反射代码,但还不错。 重要的是,您不要绑定 Visible 属性并执行解决方法,否则它将无法工作。
I've found that life is better if you assume that binding to a control's Visible property is broken, despite the fact that it sometimes works. See http://support.microsoft.com/kb/327305, which says as much (and while the KB article applies to .NET 1.0 and 1.1, it still seems to be a problem in at least 2.0).
I created a utility class for creating bindings which, among other things, gave me a centralized place to add a work-around. Instead of actually creating a binding on Visible it does two things:
This required a little reflection code, but wasn't too bad. It is critical that you don't bind the Visible property and do the work-around or it won't work.
解决方法:设置 BindingComplete 事件的 Visible 属性。
我在设置标签的 Visible 属性时遇到了同样的问题 - 始终保持 false,即使设置 Enabled 属性工作正常。
Workaround: Set the Visible property on the BindingComplete event.
I had same issue setting a label's Visible property - always stays false, even though setting the Enabled property works fine.
我刚刚在 .NET 4.7.1 和 Visual Studio 2017 中遇到了这个问题。为了解决这个问题,我将控件上的
Visible
属性更改为最初设置为True
,如下所示我之前将其设置为False
。I just hit this issue in .NET 4.7.1 and Visual Studio 2017. To fix it, I changed the
Visible
property on my control to be initially set toTrue
, as I had it asFalse
previously.要检查的事项:
希望有帮助。 您可以发布更多代码吗?
Things to check:
Hope that helps. Can you post more code?
解决方法是使用组件将数据绑定到控件的可见性属性,而不是直接绑定到控件的可见性属性。
参见下面的代码:
}
A workaround would be to use a Component to databind to a control's visiblity property instead of directly binding to the control's visibility property.
See below code:
}
这是我的转身,这可能很愚蠢,但它成功了很多次。
我在表单中放置了一个 Panel 控件,使其填充我的表单,然后将所有内容都放入该面板中。 我绑定 Visible 属性的所有控件都会看到其可见性根据 DataGridView 中的对象而变化。
Here is my turn around, it may be stupid but it worked many times.
I put one Panel control in my form, I make it to Fill my form and I put everything in that Panel. All the controls I bind the Visible property see their visibility change according to the objects in my DataGridView.
尝试在表单加载中手动绑定。
发现在表单设计器中绑定到 BindingSource 对象中的属性不起作用。 必须在代码隐藏(表单的加载事件)中设置绑定。
可能是因为在 BindingSource 使用 true 或 false 值完全初始化之前,绑定到 Visible 的属性暂时被设置为 False,因为按钮/面板/控件正在初始化,表单正在加载。
这是一个对我有用的手动绑定示例:
Try manual binding in Form Load.
Found that binding to a property in a BindingSource object in the form designer didn't work. Had to set the binding in code-behind (Form's Load event) instead.
Probably due the property bound to Visible was being set to False momentarily, as the Button/Panel/control was being initialized, as the Form was loading, before the BindingSource was fully initialized with either a true or false value.
Here is a manual binding example that worked for me: