Solved

Accessing the contents of a databound Control

Posted on 2013-12-03
4
234 Views
Last Modified: 2013-12-04
I am trying to access the contents of some labels that are databound to XML through the XAML declarations. These are readonly fields. Basically I get a date and time into one label from the XML document. I want to be able to read what that date time is on the backend so that when then form is loaded/activated/whatever a combobox will add new items based on the time (basically settings a reminder). When I access the contents they are "".

I am new to C# but doing ok. This is my first time though doing anything with Databinding. I have spent the last few days scouring the web and this is the best implementation that I can arrive at.

Here is the XAML and some code.

XAML
...
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Grid.DataContext>
            <XmlDataProvider Source="ConfigSettings.xml" XPath="Settings/Setting" />
        </Grid.DataContext>
        <Label x:Name="lblTitleData" Content="{Binding XPath=@Name}" Margin="10,10,10,0" />
        <Label x:Name="lblStartDateTimeData" Content="{Binding XPath=./StartDateTime}" HorizontalAlignment="Left" Margin="110,207,0,0" />
...

Open in new window


C# Code
...
            DateTime dteDateTime = DateTime.Now;

            // string strCombinedTime="";
            int intHoursTillEvent;

            TimeSpan ts = Convert.ToDateTime(lblStartDateTimeData.Content) - DateTime.Now;
            intHoursTillEvent = (ts.Days * 24) + ts.Hours;

            if (intHoursTillEvent >= 24)
            {
                cboReminderTime.Items.Clear();
                cboReminderTime.Items.Add("15 Minutes");
                cboReminderTime.Items.Add("1 Hour");
                cboReminderTime.Items.Add("3 Hours");
                cboReminderTime.Items.Add("6 Hours");
                cboReminderTime.Items.Add("12 Hours");
                cboReminderTime.Items.Add("24 Hours");
                cboReminderTime.Items.Add("Start Now");
            } // closes >= 24 hours
...

Open in new window

0
Comment
Question by:LouisM1979
  • 3
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39696217
I usually data-bind to a class, not the XmlDataProvider.  My choice would be to deserialize the XML into a class object, and bind the DataContext to that.
0
 

Author Closing Comment

by:LouisM1979
ID: 39696340
I will do that. By chance do you have a book or link you would reco for that? There is a lot on Google and it gets confusing at times.

Thanks.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39696369
There are a few choices for XML serialization:

XmlSerializer

http://msdn.microsoft.com/en-us/library/dsh84875(v=vs.110).aspx

using System;
using System.IO;
using System.Xml.Serialization;

// This is the class that will be deserialized. 
public class OrderedItem
{
   [XmlElement(Namespace = "http://www.cpandl.com")]
   public string ItemName;
   [XmlElement(Namespace = "http://www.cpandl.com")]
   public string Description;
   [XmlElement(Namespace="http://www.cohowinery.com")]
   public decimal UnitPrice;
   [XmlElement(Namespace = "http://www.cpandl.com")]
   public int Quantity;
   [XmlElement(Namespace="http://www.cohowinery.com")]
   public decimal LineTotal;
   // A custom method used to calculate price per item. 
   public void Calculate()
   {
      LineTotal = UnitPrice * Quantity;
   }
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      // Read a purchase order.
      t.DeserializeObject("simple.xml");
   }

   private void DeserializeObject(string filename)
   {   
      Console.WriteLine("Reading with Stream");
      // Create an instance of the XmlSerializer.
      XmlSerializer serializer = 
      new XmlSerializer(typeof(OrderedItem));

      // Declare an object variable of the type to be deserialized.
      OrderedItem i;

      using (Stream reader = new FileStream(filename, FileMode.Open))
      {
          // Call the Deserialize method to restore the object's state.
          i = (OrderedItem)serializer.Deserialize(reader);          
      }

      // Write out the properties of the object.
      Console.Write(
      i.ItemName + "\t" +
      i.Description + "\t" +
      i.UnitPrice + "\t" +
      i.Quantity + "\t" +
      i.LineTotal);
   }
}

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39696374
DataContractSerializer:


Serialization and Deserialization
http://msdn.microsoft.com/en-us/library/vstudio/ms731073(v=vs.100).aspx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now