...">

XML binding in Silverlight without nominal classes

Let's say I have a simple XML fragment: -

<root> <item forename="Fred" surname="Flintstone" /> <item forename="Barney" surname="Rubble" /> </root> 

After receiving this XML in Silverlight, I'd like to link it to XAML that ilke: -

 <ListBox x:Name="ItemList" Style="{StaticResource Items}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBox Text="{Binding Forename}" /> <TextBox Text="{Binding Surname}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

Now I can bind quite simply with LINQ to XML and the nominal class: -

 public class Person { public string Forename {get; set;} public string Surname {get; set;} } 

Can this be done without this class?

In other words, the relationship between Silverlight code and XML input is limited only to XAML, the other source code is agnostic to the set of element element attributes.

Change XSD is recommended, but in the end it is the same. XSD-> Generated Class.

Change An anonymous class does not work; Silverlight cannot bind them.

Change There must be two ways: the user must be able to edit the values, and these values ​​in XML. (Changed the original TextBlock for the TextBox in the example above.)

+3
source share
4 answers

You can do this with IValueConverter. Here is a simple one:

 public class XAttributeConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var xml = value as XElement; var name = parameter as string; return xml.Attribute(name).Value; } } 

Then in your Xaml, you can reference the type converter and pass the attribute name as a parameter:

 <ListBox x:Name="ItemList"> <ListBox.Resources> <local:XAttributeConverter x:Name="xcvt" /> </ListBox.Resources> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Converter={StaticResource xcvt}, ConverterParameter=forename}" /> <TextBlock Text="{Binding Converter={StaticResource xcvt}, ConverterParameter=surname}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

This is when you bind to the xml loaded in the XElement:

 XElement xml = XElement.Parse("<root><item forename='Fred' surname='Flintstone' /><item forename='Barney' surname='Rubble' /></root>"); ItemList.ItemsSource = xml.Descendants("item"); 

Not a super elegant binding syntax, but it works and is easier than comparing with classes.

+3
source

As far as I know, Silverlight Binding does not have the XPath properties found in WPF, so there is no good way to bind directly to XML. When I ran into this problem, I used xsd.exe for the schema to generate my classes, and then used Xml Serialization to populate them. This is not ideal, but at least I do not write and do not support classes and mappings.

0
source

Could you do something similar to what Bryant offers with a query that uses an anonymous class?

i.e:.

 var data = from c in xml.Descendants("item") select new { Forename = c.Attribute("forename").Value, Surname = c.Attribute("surname").Value }; ItemList.ItemsSource = data 

I think this should work, but I'm not somewhere where I can test it. If this is not so, someone let me know why, because now I am interested.

0
source

All Articles