Search code examples
c#visual-studiowindows-phone-7expression-blend

Binding a Style Resource Dynamically


Is there any way to dynamically bind the resource style of a rectangle ?

The following works:

<Rectangle Grid.ColumnSpan="4" Margin="0,0,0,8" Fill="{Binding RectangleColor}" />

So each list item has a different color based on its type.

The problem is that i don't like simple colors and i would like them to follow a style resource. Unfortunately this has to be done dynamically.

Trying this didnt work:

<Rectangle Grid.ColumnSpan="4" Margin="0,0,0,8" Style="{Binding RectangleStyle}" />

Solution

  • It's hard to tell what you're trying to do, but it sounds like you want a DataTemplateSelector. Of course, these don't exist in Silverlight. It seems like everyone with a blog has a way to implement it. Here is a WP7 version. If you're running WP7.1 you might run into a little bit of trouble that is covered by this stack overflow question.