Skip to main content

Microsoft Silverlight

Answered Question How do I Bind an XElement Attribute in CodeBehindRSS Feed

(0)

nbruckelmyer
nbruckel...

Member

Member

9 points

25 Posts

How do I Bind an XElement Attribute in CodeBehind

I have the following XElement document and I am trying to bind the elements and attributes to a datagrid from codebehind.  I need to do it from codebehind so I can build my datagrid dynamically.  The element is binding but the attributes are not binding correctly.  Does anyone know how to get the attributes to display.

 Here is my XElement:

XElement element = new XElement("Root",
  new XElement("Role",
    new XAttribute("CanEditUsers",false),
    new XAttribute("CanViewUsers",true),
  "Administrator"
));

Here is my code to that adds the columns to a datagrid and binds their values.

IEnumerable<XElement> childList = from el in element.Elements()
select el;

foreach (XElement e in childList)
{
 
this.dgTest.Columns.Add(
    new DataGridTextColumn
     
{
        Header = e.Name.LocalName,
        Binding =
new Binding("Value")
      });

      foreach (var a in e.Attributes())
      {
       
this.dgTest.Columns.Add(
         
new DataGridTextColumn
         
{
            Header = a.Name.LocalName,
            Binding =
new Binding("Value")
         
});
      }
}

This is what the output looks like:  (The columns are correct but the values for the attributes are not)

 

Role CanEditUsers CanViewUsers
Administrator Administrator Administrator

 

msalsbery
msalsbery

Contributor

Contributor

2066 points

379 Posts

Re: How do I Bind an XElement Attribute in CodeBehind

nbruckelmyer:
The columns are correct but the values for the attributes are not
 


You're setting the same binding on all the columns, so you get the same value in all the columns :)

You could use a converter on the attribute column bindings, passing the attribute name as the converter parameter.  An example is shown in the "Answers" section here. (thank you to Bryant for saving me from typing up an example :))

 

Mark Salsbery
Microsoft MVP - Visual C++

nbruckelmyer
nbruckel...

Member

Member

9 points

25 Posts

Re: Re: How do I Bind an XElement Attribute in CodeBehind

How can I do this from codebehind?

Jeremy Likness
Jeremy L...

Member

Member

334 points

61 Posts

Re: How do I Bind an XElement Attribute in CodeBehind

I've found it's a lot easier to define a class that represents the parsed XML values. Use LINQ to XML to load the entity/class from the XML, then bind to the class instead of trying to bind to the Xml fragments.

 

nbruckelmyer
nbruckel...

Member

Member

9 points

25 Posts

Re: Re: How do I Bind an XElement Attribute in CodeBehind

Do you know if its possible to do what I am trying.  I can't create a class based on the xml cause it's a dynamic data.

msalsbery
msalsbery

Contributor

Contributor

2066 points

379 Posts

Answered Question

Re: Re: How do I Bind an XElement Attribute in CodeBehind

nbruckelmyer:
How can I do this from codebehind?
 


 

    foreach (var a in e.Attributes())
    {
        Binding attbinding = new Binding();
        attbinding.Converter = new XAttributeConverter();
        attbinding.ConverterParameter = a.Name.LocalName;

        this.dgTest.Columns.Add(
          new DataGridTextColumn
          {
              Header = a.Name.LocalName,
              Binding = attbinding
          });
    }
  

Mark Salsbery
Microsoft MVP - Visual C++
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities