Xaml: Choose a specific color based on another property

Russ Suter
Russ Suter used Ask the Experts™
on
I have a feeling I might be going about this the wrong way. I'm hoping there's a solution to what I want to accomplish.

I have the following colors defined in a resource dictionary
    <SolidColorBrush x:Key="Add.Border" Color="#FF4BAE53"/>
    <SolidColorBrush x:Key="Add.Background" Color="#FF5BB862"/>
    <SolidColorBrush x:Key="Add.Hover" Color="#FF439D4B"/>
    <SolidColorBrush x:Key="Add.Pressed" Color="#FF38843E"/>
    <SolidColorBrush x:Key="Add.Disabled" Color="#A55BB862"/>
    <SolidColorBrush x:Key="Add.Foreground" Color="White"/>

    <SolidColorBrush x:Key="Edit.Border" Color="#FFECA040"/>
    <SolidColorBrush x:Key="Edit.Background" Color="#FFEEAC55"/>
    <SolidColorBrush x:Key="Edit.Hover" Color="#FFEA952E"/>
    <SolidColorBrush x:Key="Edit.Pressed" Color="#FFD38323"/>
    <SolidColorBrush x:Key="Edit.Disabled" Color="#A5EEAC55"/>
    <SolidColorBrush x:Key="Edit.Foreground" Color="White"/>

    <SolidColorBrush x:Key="Delete.Border" Color="#FFD33B3B"/>
    <SolidColorBrush x:Key="Delete.Background" Color="#FFD85050"/>
    <SolidColorBrush x:Key="Delete.Hover" Color="#FFC82B2D"/>
    <SolidColorBrush x:Key="Delete.Pressed" Color="#FFAA2526"/>
    <SolidColorBrush x:Key="Delete.Disabled" Color="#A5D85050"/>
    <SolidColorBrush x:Key="Delete.Foreground" Color="White"/>

Open in new window

I'm creating a custom control that will use these colors as a palette. The actual colors it will use will be determined by a property I'm calling "Purpose". That purpose will be Add, Edit, or Delete. I already have that property working. What I don't have working is being able to select the Delete.Background color if the value of "Purpose" is Delete vs. the Add.Background color if the value is Add. Is this even possible? Is there perhaps another way I should be defining my colors in the resource dictionary to make this work properly?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Personally, I would implement a UserControl with a custom design-time property called Purpose. In its setter, have it set the colors accordingly.

XAML in Host:
<PurposeButton Purpose="Add">

Open in new window


XAML in User Control (PurposeButton.xaml)
<Button x:Name="btnPurpose" ...Color Properties.. >

Open in new window


Code-Behind Snippet on PurposeButton.xaml.cs:
        public string Purpose
        {
            get
            {
                return (string)GetValue(PurposeProperty);
            }
            set
            {
                SetValue(PurposeProperty, value);

                // Here, set the button colors/properties
                btnPurpose.Background = ...
                ....
            }
        }

        public static readonly DependencyProperty PurposeProperty = DependencyProperty.Register("Purpose", 
            typeof(string), 
            typeof(PurposeButton), 
            new PropertyMetadata("Purpose", PurposeChanged));

        private static void PurposeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((PurposeButton)sender).Purpose= (string)e.NewValue;
        }

Open in new window


If you wanted to be clean about it, you could even have custom classes that contain the color schemes so you can change all the colors by choosing a different scheme.
Russ SuterSenior Software Developer

Author

Commented:
Which may work but it's not what I need. What I need is a custom control that can change its color based on the value of the Purpose property.
Commented:
Maybe I'm not understanding something - the code I provided would lead to that outcome - where setting the Purpose property would change the colors of the button...

I suppose you could also use value converters, too, but it would be a little more overhead.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Senior Software Developer
Commented:
I found what I was looking for thanks to the magic of the IValueConverter interface. It's quite neat and extremely flexible.
https://www.wpf-tutorial.com/data-binding/value-conversion-with-ivalueconverter/

Commented:
Why did you accept your own answer if I suggested using it?
Russ SuterSenior Software Developer

Author

Commented:
Since you asked... Here's an analogy.

Q: How do I learn to drive?
A: Take the car

It's technically correct but not very helpful.

Commented:
Actually, it's more like this:
YOU: How do I learn to drive?
ME: Sure, here's method A with sample code.
YOU: That might work but it's not what I want.
ME: Would you care to explain why it's not? I mean, you could also use method B.
<silence for hours>
YOU: I found what I was looking for in method B! I'm going to accept my own answer!

Nobody can read your mind, so if you don't communicate and explain why something doesn't work the way you want it to, when it actually DOES meet the original specs of the question, then I can't help you. And if you take a lead that I provide and then accept that lead as your own answer...

I guess from now on, you can count me out of commenting on your questions.
Russ SuterSenior Software Developer

Author

Commented:
No, you explained how to accomplish a similar task using a different model. Your suggestion was for a user control. I did state in my original question that I needed a custom control.

Mentioning something doesn't mean you get to lay claim to the correct answer. I had to do quite a bit of independent research, reading, and trial and error to figure out what the IValueConverter is, what it does, and how to implement it. You just said, "value converters" and think that's an answer.

I guess from now on, you can count me out of commenting on your questions.
I will look forward to your non-participation in the future.

Commented:
>  Your suggestion was for a user control. I did state in my original question that I needed a custom control.
A user control IS a custom control. That's why I provided my initial approach.

> You just said, "value converters" and think that's an answer.
No, that was a basic lead. My original comment was still the most efficient way to approach the issue, but I was waiting for you to reply as to why you didn't think that was a solution. I wasn't about to lay out how to use a value converter if there was a simple misunderstanding about my original comment. But again, there was silence for hours - no updates from you.

Had you said, "Sure, let's explore the value converter idea," I could have explained the concept and provided you with some examples and saved you from all the independent research. But hours of silence, followed by a brief comment from you that value converters is the way to go and accepting that is pretty rude.
Russ SuterSenior Software Developer

Author

Commented:
Then it seems we simply had a misunderstanding. Sorry about that. They do happen. Especially on the Internet. I've gone back and marked your response as a solution but kept mine as a solution also since it contains a link to where I found the actual implementation.

Please accept my apology. I did not intend to be rude. I simply didn't understand what you were referring to. At the time I read your response I hadn't even heard of a value converter so the term didn't stand out at all.

Commented:
Apology accepted - I appreciate the comment.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial