views:

41

answers:

1

Hey!

I am building a Silverlight application that should get it's elements from XML defined objects, but I am kinda stuck: how should I feed the Silverlight application with the data in the XML?

Example: get data from the following XML to display it in silverlight dynamically:

<?xml version="1.0" encoding="utf-8" ?>
<item>

0 item0 1 item1

I need to retrieve the id (0, 1, ...) and the name (item0, item1, ...) and generate a XAML with the data:

For example to insert that name (item0, item1, ...) as the Title of a TextBlock (instead of "__" in the next XAML):

<TextBlock x:name="title" Title="______"> 

I guess it's really easy, but I don't see it :-)

What would be a good approach? Can this be done by transforming XML to XAML using XSLT?
Any other suggestion?

+2  A: 

OK based on your comment I feel like you need to sit back and read some Silverlight demos/documentation :)

What you're describing is a fundamental ability in Silverlight : templatizing lists and databinding.

For example, you can define a ListBox like so:

<ListBox ItemsSource="{Binding MyList}">
  <ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding MyTextProperty}"/>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

The MyList could be in a ViewModel property (or any other collection you can assign to the DataContext). Silverlight will then duplicate your template for each item, assigning the DataContext of each duplicate to the next item.

A good place to start learning this technique is a tutorial from Scott Gu.

Bobby
Thanks for the tip! Let me check that :)
Aidenn
That tutorial saved me a lot of time, it's really into the details and very well explained. Extremely useful, thank you!
Aidenn