I want to bind a control fill color to a boolean in c#, so if it is false, the color is Red and if it is true, the color is Green.
Pretty new to XAML, but want to get into good habits from the start.
Thanks,
Ben
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
View models exist to convert data into a format that the UI can use, so create a property of type
Colorin the ViewModel for the form that does the logical conversion from the stored boolean value to aColorvalue. Call this property ‘DisplayColor’.Use the
INotifyPropertyChangedinterface on the ViewModel to raise events on the ‘DisplayColor’ property every time the boolean value changes colour (if it changes over time). This will ensure that the UI updates itself whenever the boolean value changes.Then bind the color property of the control to the view model’s new ‘DisplayColor’ property (you will have set the
DataContextto the ViewModel, presumably).I did this for the first time just yesterday 🙂