0

I am building an application using Prism 6, but I am new to MVVM, data binding and all these design patterns. In the application, I have a requirement to display different colours depending on the value of some of the properties of my view model.

To do so, I have a few SolidColorBrush in a resource dictionary like so: <SolidColorBrush x:Key="{x:Static status:Status.notViewed}">#FFe74856</SolidColorBrush>

Then in my view, I use: prism:ViewModelLocator.AutoWireViewModel="True" to have it wired to the corresponding view model. I also use binding on a canvas (but that could be a grid or anything else, I don't really care) like this: <Canvas Background="{Binding B}" Margin="5" />. As a side note, I have other elements bound to other properties, like a textblock bound to a string to display the date, and that works fine.

Now, in my view model I create the corresponding property B for the binding public SolidColorBrush B = (SolidColorBrush)Application.Current.Resources[Status.notViewed];.

When I run my application, the canvas stays white... However I am quite sure that B contains a brush because I output its content in the constructor of the view model (Console.WriteLine("Color: {0}", B.ToString());0 and I get the right value.

What am I doing wrong?

Thanks

Alexis Le Compte
  • 337
  • 1
  • 3
  • 13

2 Answers2

0

The expression

public SolidColorBrush B
    = (SolidColorBrush)Application.Current.Resources[Status.notViewed];

declares a public field, not a property. A property would look like this:

public SolidColorBrush B { get; set; }
    = (SolidColorBrush)Application.Current.Resources[Status.notViewed];

Data binding in WPF works with public properties only.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Not only you helped me solving my problem, but you also made me realise that I didn't really know the difference between fields and properties. I guess I need to go back to the basics :) – Alexis Le Compte Apr 29 '16 at 08:00
-1

Please you can use the easier way to achieve this is to IValueConverter like by below

class ConditionToBackgroundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value.ToString() == "Condition over here")
            {
                return new SolidColorBrush(System.Windows.Media.Colors.Aqua);
            }else
            {
                return new SolidColorBrush(System.Windows.Media.Colors.Blue);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

Then on your binding you do something like this

<Canvas Background="{Binding Condition,Converter={StaticResource ConditionToBackgroundConverter }}" Margin="5" />

But dont forget to include ConditionToBackgroundConverter as a resource in your window.resource or app.xaml

Note Condition in the above binding is the condtion will want to change color for.It SHOULD be declay as a property in your viewmodel.

Hope this will help

suulisin
  • 1,414
  • 1
  • 10
  • 17
  • Thanks for the answer, but that would have probably not worked. I hadn't set the setter and the getter for my property, making it a field, and therefore, I couldn't bind it as @Clemens pointed it out. Moreover, I already have a dictionary of brushes, I didn't want to create a converter, that would have defeated the point of having the dictionary and using MVVM (some brushes stored in a dictionary, others defined in the code => less separation, more difficult to maintain). PS: Don't take that as a criticism, and I am not the one who downvoted. – Alexis Le Compte Apr 29 '16 at 08:47
  • OK thank who ever down voted my answer needs to explain why the down vote becos my answer is another way to of solving the problem.What i did what to give a guide which can be refactor to solve the particular problem. – suulisin Apr 29 '16 at 10:26