VS2010 中的 winUserControl - 属性在设计器中不可见
我的 Visual Studio 2010 Express 环境有一个问题:当我设计自己的 UserControl 时,在属性网格中我看不到该控件的公共属性。然而,它们在引用此控件的项目中是可见的。
由于它是 Express 版本,我创建新的空项目,然后向其中添加新的 UserControl。
然后,为了进行测试,我放置了以下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Project1
{
public partial class UserControl1 : UserControl
{
private int myNumber;
[Browsable(true)]
public int MyNumber
{
get
{
return myNumber;
}
set
{
myNumber = value;
}
}
public UserControl1()
{
InitializeComponent();
}
}
}
在 VS 2008 中,我记得,即使没有 [Browsable(true)]
属性,也应该足以在属性网格中显示 MyNumber 属性。然而,在 VS 2010 中,当我在解决方案资源管理器中双击 UserControl1.cs 并查看“属性”时,我看不到 MyNumber。
当我在另一个项目中引用和使用此控件时,可以访问它的属性。
我尝试完全重新安装VS 2010环境,包括SP1,但没有成功。您知道可能出了什么问题吗?
顺便说一句:这些属性都不起作用:
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Bindable(true)]
最好的问候,
马尔钦
I have a problem with (I suppose) my Visual Studio 2010 Express environment: when I design my own UserControl, in Properties grid I can't see public properties of that control. They are however visible in the project, that reference this control.
As it's Express Edition, I create new empty project, then add new UserControl to it.
Then, for a test, I put following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Project1
{
public partial class UserControl1 : UserControl
{
private int myNumber;
[Browsable(true)]
public int MyNumber
{
get
{
return myNumber;
}
set
{
myNumber = value;
}
}
public UserControl1()
{
InitializeComponent();
}
}
}
In VS 2008, as I remember, that should be enogh to show MyNumber property in Properties grid, even without [Browsable(true)]
attribute. In VS 2010 however, when I double click UserControl1.cs in Solution Explorer and look in Properties, I don't see MyNumber.
When I reference and use this control in another project, there is an access to it's properties.
I've tried to competly reinstall VS 2010 environment, including SP1, but with no success. Do you have any idea what can be wrong?
By the way: none of these attributes are working, either:
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Bindable(true)]
Best regards,
Marcin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信这是 VS2010 的正常行为,并假设这是设计使然。对于我来说,2010 Ultimate 的表现是一样的。当您将
UserControl1
放置在表单上时,您将看到其自定义属性。我的猜测是,这是因为当您设计控件时,还没有控件的实例(它甚至可能尚未编译)。您看到的是
UserControl
的一个实例。当您编译控件并将其添加到窗体时,设计器将创建控件的实例,以便可以查看和操作其属性。I believe this the normal behavior of VS2010 and assume it's by design. It behaves the same for me in 2010 Ultimate. When you place
UserControl1
on a form, you'll see its custom properties.My guess is this is because when you're designing the control, there is no instance of your control yet (it may not have even been compiled). What you're looking at is an instance of
UserControl
. When you compile your control and then add it to a form, the designer creates an instance of your control, so its properties can be seen and manipulated.我以前没有使用过 [Browsable] 标签。然而,下面是我在我的一个项目中使用的示例。
我猜你需要添加一个类别。
I haven't used the [Browsable] tag before. However below is an example of what I'm using in one of my projects.
I'm guessing you need to add a category.
由于 VS 在 Designer 中处理 ascx 的方式,这将不起作用。有关详细信息,请参阅这个优秀的回答 SO。
如果答案不是您所期望的,您仍然可以将 .ascx'es 迁移到用户控件库,如我在 我的博客。
如果可以选择,我会将所有 ascx 代码重新设置为 自定义 Web 服务器控件。
This won't work due to the way VS handles ascx'es in Designer. For details, see this excellent answer on SO.
If the answer is not what you expected, you can still migrate the .ascx'es to a User Control library as I described in my blog.
If I had the choice, I would start over all my ascx code as Custom Web Server Controls.