[英]C# NHibernate: How to do constructor injection into classmapping derived class?
[英]In C# how do I access the properties of a derived class from the derived class constructor?
我有一个DataGridView
派生的控件,其中定义了一些新属性。 它们在属性检查器中按预期显示并输入了连接字符串值,但是当我编译程序时,如果我在以下位置的构造函数上插入了一个中断: if (ConnectionString == "" )
-它显示ConnectionString
为null,而connectionString
确实为null 。 我意识到我应该在编写完此代码后对null进行测试,但要点是该属性中包含字符串。
目的是将table和connectionString
定义为从中填充表和属性的属性。 我究竟做错了什么?
namespace ASControls
{
public partial class ASDataGridView : DataGridView
{
private string connectionString;
// Declares the property.
[Description("Set the Connection String for the datbase concerned"),
Category("Data")]
public string ConnectionString
{
get { return connectionString; }
set {connectionString= value; }
}
public ASDataGridView()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
if (ConnectionString == "" )
{
MessageBox.Show("You must set the connection string in order to use an ASDataGridView control");
return;
} else {
InitData(ConnectionString);
}
}
...
Simon谈论的是在设计器中您无法调用数据库的事实。 解决方案是:
if (IsInDesignMode) // Code runs in Blend --> create design time data.
{
StatBarTextProp = "IN Design mode";
}
else // Code runs "for real"
{
StatBarTextProp = /* Get from DB here*/ ;
}
现在,您的运行时很高兴从数据库中获取内容,并且您的设计将具有默认设置。
是的,您可以在这里阅读更多内容,但基本上是:
DesignerProperties.IsInDesignMode附加属性
.NET Framework 4.5其他版本此主题尚未评级-评价此主题获取或设置DependencyObject是否在设计器的上下文中运行。
命名空间:System.ComponentModel
程序集:PresentationFramework(在PresentationFramework.dll中)XAML的XMLNS:未映射到xmlns。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.