❔ 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
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.but i do not want to use stings or... ints for it
Bind Rectangle.Fill directly to the Enum property and then use a converter
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
You dont, color logic is then in the converter class, which is code as well.
Actually, this is even the way you should go
why?
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.
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?
It is also not a huge effort too. - Several ways will work. - I you wanna go MVVM, i recommend using converters.
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.
Well maybe you can try to use the following solution from Brian..
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
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
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
True that.
Maybe i missunderstood that sentence. 🙂
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
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/
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 :(
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.It's really interesting hack.
Lol I solved my problem just by deleting Fill in Rectangle. Somereason, it always added by default.
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.