Silverlight ~ MVVM ~ Dynamic setting of Style property based on model value
Posted
by eponymous23
on Stack Overflow
See other posts from Stack Overflow
or by eponymous23
Published on 2010-06-08T17:18:12Z
Indexed on
2010/06/08
22:02 UTC
Read the original article
Hit count: 159
I have a class called Question
that represents a question and it's answer. I have an application that renders an ObservableCollection of Question
objects. Each Question
is rendered as a StackPanel that contains a TextBlock for the question verbiage, and a TextBox for the user to enter in an answer. The questions are rendered using an ItemsControl, and I have initially set the Style of the Questions's StackPanel using a StaticResource key called 'IncorrectQuestion' (defined in UserControl.Resources section of the page). In the UserControl.Resources section, I've also defined a key calld 'CorrectQuestion' which I need to somehow apply to the Question's StackPanel when the user correctly answers the question. My problem is I'm not sure how to dynamically change the Style of the StackPanel, specifically within the constraints of a ViewModel class (i.e. I don't want to put any style selection code in the View's code-behind). My Question
class has an IsCorrect
property which is accurately being set when the correction is answered. I'd like to somehow reflect the IsCorrect
value in the form of a Style selection. How do I do that?
© Stack Overflow or respective owner