Martyn Ball
Martyn Ball

Reputation: 4895

Bind SolidColorBrush to Background, binding error

It seems in binding some data wrong, can someone help me out where i'm going wrong though as I can't figure it out.

Don't really need to show too much here, this is the Binding, I have tested the background by removing the background and just putting a colour in there, that works.

Edit: But with the binding there is no colour being rendered!

<Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="{Binding Color1}" />
                </Setter.Value>
            </Setter>

This is the class where the colour is set, I use SolidColorBrush as this is what the Background property expects:

public class notificationObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }

        }

        private string _icon;
        private string _message;
        private string _detail;
        private SolidColorBrush _color1;
        private SolidColorBrush _color2;

        public string Icon
        { get { return _icon; } set { _icon = value; OnPropertyChanged("Icon"); }}
        public string Message
        { get { return _message; } set { _message = value; OnPropertyChanged("Message"); } }
        public string Detail
        { get { return _detail; } set { _detail = value; OnPropertyChanged("Detail"); } }
        public SolidColorBrush Color1
        { get { return _color1; } set { _color1 = value; OnPropertyChanged("Color1"); } }
        public SolidColorBrush Color2
        { get { return _color2; } set { _color2 = value; OnPropertyChanged("Color2"); } }

        public notificationObject newNotification(int type, string message, string detail)
        {
            //Create new instance of object
            notificationObject x = new notificationObject();
            switch (type)
            {
                case 1:
                    //Fatal
                    x.Icon = "";
                    x.Message = message;
                    x.Detail = detail;
                    x.Color1 = new SolidColorBrush(Color.FromArgb(0, 170, 60, 18));
                    x.Color2 = new SolidColorBrush(Color.FromArgb(0, 238, 78, 16));
                    return x;
                case 2:
                    //Fatal
                    x.Icon = "";
                    x.Message = message;
                    x.Detail = detail;
                    x.Color1 = new SolidColorBrush(Color.FromArgb(0, 170, 60, 18));
                    x.Color2 = new SolidColorBrush(Color.FromArgb(0, 238, 78, 16));
                    return x;
                case 3:
                    //Unauthorized
                    x.Icon = "";
                    x.Message = message;
                    x.Detail = detail;
                    x.Color1 = new SolidColorBrush(Color.FromArgb(0, 170, 60, 18));
                    x.Color2 = new SolidColorBrush(Color.FromArgb(0, 238, 78, 16));
                    return x;
                case 4:
                    //Warning
                    x.Icon = "";
                    x.Message = message;
                    x.Detail = detail;
                    x.Color1 = new SolidColorBrush(Color.FromArgb(0, 241, 176, 24));
                    x.Color2 = new SolidColorBrush(Color.FromArgb(0, 205, 152, 28));
                    return x;
                case 5:
                    //Warning
                    x.Icon = "";
                    x.Message = message;
                    x.Detail = detail;
                    x.Color1 = new SolidColorBrush(Color.FromArgb(0, 41, 161, 213));
                    x.Color2 = new SolidColorBrush(Color.FromArgb(0, 36, 142, 184));
                    return x;
            }
            //Can't find error code
            x.Icon = "";
            x.Message = "Unable to find requested error code!";
            x.Detail = "";
            x.Color1 = new SolidColorBrush(Color.FromArgb(0, 170, 60, 18));
            x.Color2 = new SolidColorBrush(Color.FromArgb(0, 238, 78, 16));
            return x;
        }
    }

And obviously I set my DataContext to the instance of this class, I have got other bindings where I bind to the Message property and this works fine, so i'm sure it's something to do with the data type being binded.

Upvotes: 1

Views: 575

Answers (2)

Peter Duniho
Peter Duniho

Reputation: 70701

Your code attempts to create a whole new SolidColorBrush object, providing the Color1 property value as the Color property for the new SolidColorBrush. Except that the Color1 property is itself a SolidColorBrush and not a Color as needed for the SolidColorBrush.Color property.

It seems to me that the most obvious fix would be to just set the Background property directly from the Color1 property:

<Setter Property="Background" Value="{Binding Color1}"/>

If for some reason you really want a whole new SolidColorBrush object, you would have to initialize using an actual Color value. For example:

<Setter Property="Background">
            <Setter.Value>
                <SolidColorBrush Color="{Binding Color1.Color}" />
            </Setter.Value>
        </Setter>

Upvotes: 2

TheInnerLight
TheInnerLight

Reputation: 12184

Your Color1 property is of type SolidColorBrush but you are binding to the Color property of your SolidColorBrush which has type System.Windows.Media.Color.

You could bind the background property directly to the SolidColorBrush you are creating at the moment or you could change those properties to expose a System.Windows.Media.Color instead and keep your XAML as it is now.

Note, since you have setters to change those colours, that you'll need to implement INotifyPropertyChanged if you want any changes to be reflected in the view.

Upvotes: 1

Related Questions