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

Occasional Contributor

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";
                case "Not connected state":
                    colorKey = "DeviceNotConnectedState";
                case "Alarm state":
                    colorKey = "DeviceAlarmState";
                case "Error state":
                    colorKey = "DeviceErrorState";
                    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?


1 Reply

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