Forum Discussion

Daniel_Donnelly_Jr's avatar
Daniel_Donnelly_Jr
Copper Contributor
Dec 18, 2022

How does one databind a "Color function" that takes a parameter an already-bound object?

Here is the function I want to bind to:

 

 

 

    public Color DeviceStatusColor(Device device)
        {
            string colorKey;
    
            switch (device.Status)
            {
                case "Connected and OK state":
                    colorKey = "DeviceConnectedAndOkState";
                    break;
    
                case "Not connected state":
                    colorKey = "DeviceNotConnectedState";
                    break;
    
                case "Alarm state":
                    colorKey = "DeviceAlarmState";
                    break;
    
                case "Error state":
                    colorKey = "DeviceErrorState";
                    break;
    
                default:
                    return new Color(0, 0, 0);
            }
    
            return (Color) Application.Current.Resources[colorKey];
        }

 

 

 

 

It sits in my DevicesViewModel.cs class. I would like to do something like "Stroke={Binding DeviceStatusColor, Parameter=Device}" in XAML, but I know that's not correct and there's no web articles on how to do this other than a Data-binding Converter. I think that's overkill. I just want to pass a parameter.

If Converter is the way to go, then how would I accomplish that?

 

  • Hi Daniel_Donnelly_Jr,

    Thanks for posting your issue here.

    However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
    Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
    Best Regards,
    Lan Huang

Resources