C
C#16mo ago
Mideks

❔ How can I bind Fill Color to Enum Value?

I have been tried someting like this(check my pictures) but it does not work. What I do wrong?
21 Replies
Mideks
Mideks16mo ago
Seems problem in binding to enum. for strings it works as intended. (image just for test) I change TeteValue type and initialization for cell. And it work.
Mideks
Mideks16mo ago
but i do not want to use stings or... ints for it
Hannsen
Hannsen16mo ago
Bind Rectangle.Fill directly to the Enum property and then use a converter
Mideks
Mideks16mo ago
I would not like to take out the logic of determining the color in the code I already thought about putting it into the converter, but it seems to me that this is wrong
Hannsen
Hannsen16mo ago
You dont, color logic is then in the converter class, which is code as well. Actually, this is even the way you should go
Mideks
Mideks16mo ago
why?
Hannsen
Hannsen16mo ago
Because it should not be the ViewModels business, of how data is rendered/shown. - This is View business. And in the view, you use a converter for it.
Mideks
Mideks16mo ago
it's like if I declared the color in vm Okay, maybe that's not the worst idea. but then I won't be able to use resources to define colors. what if i want to add a new theme?
Hannsen
Hannsen16mo ago
It is also not a huge effort too. - Several ways will work. - I you wanna go MVVM, i recommend using converters.
Mideks
Mideks16mo ago
Okay, maybe not. I just really don't think it's a good idea to define something like that in code. because it refers to the presentation. of course i could be wrong but I was still wondering why I can't just bind like this to an enum? I was looking for information about this, and in 2 sources I found just such a code.
Hannsen
Hannsen16mo ago
Well maybe you can try to use the following solution from Brian..
Hannsen
Hannsen16mo ago
Brian Lagunas
Brian Lagunas
A Better Way to Data Bind Enums in WPF - Brian Lagunas
Have you needed to data bind enums in WPF? Well, if your write WPF applications, you bet your ass you have! Learn a better way to bind to your enums in WPF
Hannsen
Hannsen16mo ago
But i am not sure, if you then would need a "Converter" anyway... But, if you would really like to keep the logic in the viewmodel. Then a way would be: - Add another property "public string TesteValueColor" without the ObservableProperty annotation and just a getter. - Put the color conversion into the getter of that property. - Add the additional annotation [NotifyPropertyChangedFor(nameof(TesteValueColor))] to the TesteValue Enum property. With that, it would work as well
Mideks
Mideks16mo ago
this is a great solution, but not for my problem. in any case, thanks for the link, I once needed this, now I know how to do it. why did you decide that I want to define logic in the view model? on the contrary, I wanted to avoid it. it seemed to me that determining the color of the display is not the responsibility of vm so I decided to try to bind the color depending on the state in the view
Hannsen
Hannsen16mo ago
True that. Maybe i missunderstood that sentence. 🙂
Want results from more Discord servers?
Add your server