C
C#2y 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
MideksOP2y 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
MideksOP2y ago
but i do not want to use stings or... ints for it
Hannsen
Hannsen2y ago
Bind Rectangle.Fill directly to the Enum property and then use a converter
Mideks
MideksOP2y 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
Hannsen2y 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
MideksOP2y ago
why?
Hannsen
Hannsen2y 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
MideksOP2y 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
Hannsen2y ago
It is also not a huge effort too. - Several ways will work. - I you wanna go MVVM, i recommend using converters.
Mideks
MideksOP2y 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
Hannsen2y ago
Well maybe you can try to use the following solution from Brian..
Hannsen
Hannsen2y 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
Hannsen2y 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
MideksOP2y 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
Hannsen2y ago
True that. Maybe i missunderstood that sentence. 🙂
Mideks
MideksOP2y ago
I meant that it seems to me that the converter is more relevant to vm than to view. at least because it's code, not markup
Hannsen
Hannsen2y ago
Good point. - Cool thing in WPF is, i mostly always actually derive my Converters from MarkupExtension Here is why: http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/
Mideks
MideksOP2y ago
ooh, that sounds pretty interesting. but I guess I'll put it off until tomorrow. thanks for your answers. superficially looking at what it is, it seems to me that this would greatly complicate my solution, which initially seemed to me quite simple :(
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Mideks
MideksOP2y ago
It's really interesting hack. Lol I solved my problem just by deleting Fill in Rectangle. Somereason, it always added by default.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server