通过循环将项添加到DataContext中

时间:2021-08-29 06:56:35

I have following code in C#, WPF:

我在C#,WPF中有以下代码:

base.DataContext = new DataTemplate[]
                {
                    new DataTemplate
                    {
                        lblText = "First",
                        txtBoxContent = ""
                    },

                    new DataTemplate
                    {
                        lblText = "Second",
                        txtBoxContent = "Something"
                    }
                };

but i need to fill DataContext dynamically from database. My idea looks like this:

但我需要从数据库动态填充DataContext。我的想法是这样的:

base.DataContext = new DataTemplate[]
        {
            for(int i = 0; i< dsTmp.Tables[0].Rows.Count; i++)
            {
                new DataTemplate
                {
                    lblText = "Count: ",
                    txtBoxContent = dsTmp.Tables[0].Rows[i][0].ToString();
                }
            }
        };

When i type this, it yells some syntax errors on me;

当我输入它时,它会对我产生一些语法错误;

Could anybody tell me, how to write it correctly?

谁能告诉我,怎么写得正确?

4 个解决方案

#1


3  

You can't have code inside object initializer syntax. Why not simply do this:

您不能在对象初始化程序语法中包含代码。为什么不简单地这样做:

   var list = new DataTemplate[dsTmp.Tables[0].Rows.Count];
   for(int i = 0; i< dsTmp.Tables[0].Rows.Count; i++)
    {
        var item = new DataTemplate
        {
            lblText = "Count: ",
            txtBoxContent = dsTmp.Tables[0].Rows[i][0].ToString();
        };
        list[i] = item;
    }

    this.DataContext = list;

#2


1  

The error that ; is missing is bit misleading. The actual problem is that you are trying to create an array of DataTemplate with the loop, You can't use loop in array/object initialization. Try the following.

错误;缺少有点误导。实际的问题是你试图用循环创建一个DataTemplate数组,你不能在数组/对象初始化中使用循环。请尝试以下方法。

DataTemplate[] tempDataTemplate = new DataTemplate[ds.Temp.Tables[0].Rows.Count]();
for(int i = 0; i< dsTmp.Tables[0].Rows.Count; i++)
            {

             tempDataTemplate[i] =   new DataTemplate
                                             {
                                             lblText = "Count: ",
                                             txtBoxContent = dsTmp.Tables[0].Rows[i][0].ToString();
                                             };
            }

base.DataContext = tempDataTemplate;

#3


1  

MBen and Habib have already answered why the for is failing, because you can't do a loop in an object initializer and have provided loop alternatives.

MBen和Habib已经回答了for for失败的原因,因为你无法在对象初始化器中进行循环并提供了循环替代方案。

Alternatively you can use linq to perform an initialization.

或者,您可以使用linq执行初始化。

this.DataContext=dsTmp.Tables[0].Rows.Select(
  x=>new DataTemplate { 
                        lblText = "Count: ",
                        txtBoxContent=x[0].ToString()
                      }).ToArray(); 

#4


0  

i dont know what you wanna achieve, but did you ever try mvvm with viewmodel first approach?

我不知道你想要实现什么,但你有没有试过mvvm与viewmodel第一种方法?

create a viewmodel class, e.g. MyData with 2 public properties MyText, MyContent. create a collection of these objects and fill this from your database.

创建一个viewmodel类,例如MyData有2个公共属性MyText,MyContent。创建这些对象的集合并从数据库中填充它。

at least you need an itemscontrol with itemssource binding set to your collection and a datatemplate for your MyData object.

至少你需要一个itemcontrol,其中itemssource绑定设置为你的集合,并且你需要一个数据模板来存放你的MyData对象。

<DataTemplate DataType="{x:Type local:MyData}">
  <view:MyDataViewControl />
</DataTemplate>

now you see all your dynamic objects in your itemscontrol.

现在,您可以在itemscontrol中看到所有动态对象。

#1


3  

You can't have code inside object initializer syntax. Why not simply do this:

您不能在对象初始化程序语法中包含代码。为什么不简单地这样做:

   var list = new DataTemplate[dsTmp.Tables[0].Rows.Count];
   for(int i = 0; i< dsTmp.Tables[0].Rows.Count; i++)
    {
        var item = new DataTemplate
        {
            lblText = "Count: ",
            txtBoxContent = dsTmp.Tables[0].Rows[i][0].ToString();
        };
        list[i] = item;
    }

    this.DataContext = list;

#2


1  

The error that ; is missing is bit misleading. The actual problem is that you are trying to create an array of DataTemplate with the loop, You can't use loop in array/object initialization. Try the following.

错误;缺少有点误导。实际的问题是你试图用循环创建一个DataTemplate数组,你不能在数组/对象初始化中使用循环。请尝试以下方法。

DataTemplate[] tempDataTemplate = new DataTemplate[ds.Temp.Tables[0].Rows.Count]();
for(int i = 0; i< dsTmp.Tables[0].Rows.Count; i++)
            {

             tempDataTemplate[i] =   new DataTemplate
                                             {
                                             lblText = "Count: ",
                                             txtBoxContent = dsTmp.Tables[0].Rows[i][0].ToString();
                                             };
            }

base.DataContext = tempDataTemplate;

#3


1  

MBen and Habib have already answered why the for is failing, because you can't do a loop in an object initializer and have provided loop alternatives.

MBen和Habib已经回答了for for失败的原因,因为你无法在对象初始化器中进行循环并提供了循环替代方案。

Alternatively you can use linq to perform an initialization.

或者,您可以使用linq执行初始化。

this.DataContext=dsTmp.Tables[0].Rows.Select(
  x=>new DataTemplate { 
                        lblText = "Count: ",
                        txtBoxContent=x[0].ToString()
                      }).ToArray(); 

#4


0  

i dont know what you wanna achieve, but did you ever try mvvm with viewmodel first approach?

我不知道你想要实现什么,但你有没有试过mvvm与viewmodel第一种方法?

create a viewmodel class, e.g. MyData with 2 public properties MyText, MyContent. create a collection of these objects and fill this from your database.

创建一个viewmodel类,例如MyData有2个公共属性MyText,MyContent。创建这些对象的集合并从数据库中填充它。

at least you need an itemscontrol with itemssource binding set to your collection and a datatemplate for your MyData object.

至少你需要一个itemcontrol,其中itemssource绑定设置为你的集合,并且你需要一个数据模板来存放你的MyData对象。

<DataTemplate DataType="{x:Type local:MyData}">
  <view:MyDataViewControl />
</DataTemplate>

now you see all your dynamic objects in your itemscontrol.

现在,您可以在itemscontrol中看到所有动态对象。