【发布时间】:2016-08-26 09:35:37
【问题描述】:
我有一个很奇怪的问题。
这是我定义的一个类:
public class HeaderTagControlsPair
{
public TextBlock HeaderTextBlock = new TextBlock();
public ComboBox TagComboBox = new ComboBox();
public RowDefinition Row = new RowDefinition();
}
现在,我要创建这个类的一个对象并初始化它:
HeaderTagControlsPair example = new HeaderTagControlsPair
{
HeaderTextBlock.Text = "test"
};
我做不到。我得到了这三个错误:
Error 1 Cannot initialize type 'CSV_To_Tags_App.HeaderTagControlsPair' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
Error 2 Invalid initializer member declarator
Error 3 The name 'HeaderTextBlock' does not exist in the current context
我不知道为什么会这样,我只是在使用简单的对象初始化。我做错了什么?
【问题讨论】:
-
至少发生其中一个错误是因为您在未初始化的情况下访问了
HeaderTextBlock的Text属性。考虑尝试HeaderTagControlsPair example = new HeaderTagControlsPair();,然后设置单独的值,或者使用“test”字符串(例如tb)声明一个新的TextBlock元素并在声明中设置HeaderTextBlock.Text = tb。
标签: c# wpf class initialization