简体   繁体   中英

Add items into DataContext via loop

I have following code in 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:

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?

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;

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.

Alternatively you can use linq to perform an initialization.

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

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[] 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;

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

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

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

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

now you see all your dynamic objects in your itemscontrol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM