Accessing the contents of a databound Control

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.


            <XmlDataProvider Source="ConfigSettings.xml" XPath="Settings/Setting" />
        <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.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

Who is Participating?
Bob LearnedConnect With a Mentor Commented:
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.
LouisM1979Author Commented:
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.

Bob LearnedCommented:
There are a few choices for XML serialization:


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

// This is the class that will be deserialized. 
public class OrderedItem
   [XmlElement(Namespace = "")]
   public string ItemName;
   [XmlElement(Namespace = "")]
   public string Description;
   public decimal UnitPrice;
   [XmlElement(Namespace = "")]
   public int Quantity;
   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.

   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.
      i.ItemName + "\t" +
      i.Description + "\t" +
      i.UnitPrice + "\t" +
      i.Quantity + "\t" +

Open in new window

Bob LearnedCommented:

Serialization and Deserialization
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.