Dynamically create a DataTempalte in Silverlight code.

The issue is following: we have a ListBox and for some reason we need to create a ListBox.ItemTemplate in code. In my case, I had to build a ListBox were each item is a button and I don’t know the template of it. I got it from other service. So there is no choose to do it on some other case, except of whole refactoring of architecture.

First of all, I’d like to maintain: don’t do it!
J It’s not a juke, there are lots of other possibility to avoid this situation, such as: use a converters/multy-converters, use predefined templates, eventually different styles for your ListBox. However, it’s not always possible.

First way: use a XamlLoader.

Just create necessary template like a string in code behind and load it

(DataTemplate) XamlReader.Load(“<DataTemplate>…</DataTemplate>”);

Note: don’t forget about necessary namespace. But in our case we don’t know what template we have.

Second way: use a ContentControl.

I used a ContentControl (I placed here a button with a DataTemplate) and Loaded event.

In test application I just loaded a ControlTemplate for button from Resources. Then added a button with click handler:

Eventually, this button I added like a DataTemplate to ListBoxItem.

Hope it’ll help.

       I haven’t been here around 5 months L It’s really sad! But it’s not only because I’m a lazy. There were a lot of changes in my life during this time. I completely went to Silverlight/WP7 development, change a job (ever twice!) and…change a country. Now I’m working, living and drinking a Belgium beer in Antwerp, Belgium. This was agenda and now let’s does it more extended about everything.

I really like Silverlight, enjoy what you can do with it and how. Everything can be done very fast and in OOP+TDD paradigms. In other hand I completely misunderstand Microsoft and their politics with this technology (I suppose I’ll write a separate post about this one). Maybe they think that everything should be possible on Windows 8? – Foolish idea. They gave us a Visual Studio plugin for Python, Ruby and PHP, now they negotiate the html 5. Of course it’s good to have lots of possibilites, however developers and especially business don’t understand where is a future, what is a main way! Ok, it’s a long story which I promise to tell soon.

My chose is a Silverlight and a WP7 development now, but I’m more and more like iOS+Mac development.

To the best of my belief life should be funny, interesting and full of positive emotion! If nothing happens in your life – believe me, you are doing something wrong. If you e-mail box/Skype/whatever is empty, there is only one reason: you’re not needed.

Firstly I worked for one of the biggest IT company in Donetsk, Ukraine. It was OK, except that my application was completely on Ext JsJ I hate javascript and don’t want to combine my life with it. So I had no choose.

Then I was hired for Belgium ItemSolutions company. I’m happy here. I suppose that outsourcing is wrong way for company and particularly me. I’m truly belief on own products and Silverlight and everything I found here. And every more: good people and atmosphere, interesting projects, and company which is “knowledge-driven instead of business-driven.” Eventually I belief in a company and projects part of which I’m.

Of course, at the end I’d like to maintain few words about my new country:

Enjoy your life!