rozy
rozy

Reputation: 63

How data can be bound in windows phone 8

I want to bind the data in to list, the data can be parsed through xml and stored in local variables.

Next I want to bind these data in to list and stored in isolated storage. Please help me out.

 if (e.Result != null)
  {
                string serviceDate, name, taskname;
                XDocument doc;
                doc = XDocument.Parse(e.Result);
    foreach (var absentDate in doc.Element("Classworks").Descendants("Classwork"))
                {
                    serviceDate = absentDate.Element("ClassworkDate").Value.ToString();
                    name = absentDate.Element("Subject").Value.ToString();
                    taskname = absentDate.Element("Task").Value.ToString();
                }


            }
            else
            {
            }

Upvotes: 1

Views: 61

Answers (1)

har07
har07

Reputation: 89285

These are the steps you need to perform approximately :

  1. Create a model to represent single data in List, say it MyModel. If MyModel property values is not static (can be changed at runtime), you may also need to implement INotifyPropertyChanged in the model.
  2. Create a property of type ObservableCollectoin<MyModel>, say it MyItemsSource
  3. Put a LongListSelector or similar user control that capable of displaying multiple data on the Page. Bind the control's ItemsSource to MyItemsSource property
  4. Set the Page's DataContext properly.
  5. Populate MyItemsSource in code, you can do this in foreach part shown in question.
  6. Later, when you need to save the data to IsolatedStorage, the data is available in MyItesSource property. Just save it.

Step 1

public class MyModel
{
    public string ClassworkDate { get; set; }
    public string Subject { get; set; }
    public string Task { get; set; }
}

Step 2

public ObservableCollection<MyModel> MyItemsSource { get; set; } 

Step 4

public MainPage()
{
    InitializeComponent();
    MyItemsSource = new ObservableCollection<MyModel>();
    this.DataContext = this;
}

Step 5

foreach (var absentDate in doc.Element("Classworks").Descendants("Classwork"))
{
    serviceDate = absentDate.Element("ClassworkDate").Value.ToString();
    name = absentDate.Element("Subject").Value.ToString();
    taskname = absentDate.Element("Task").Value.ToString();
    var newModel = new MyModel{
                                 ClassworkDate = serviceDate,
                                 Subject = name,
                                 Task = taskname
                               };
    MyItemsSource.Add(newModel);
}

Above are codes related to logic in steps I explained before. With those codes setup, your data is ready to be bound. Note that in this example I didn't implement INotifyPropertyChanged for brevity.

Upvotes: 1

Related Questions