-
Notifications
You must be signed in to change notification settings - Fork 393
Binding units to (WPF) controls #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, I have not yet tried anything like this myself, sorry. Have you looked into using https://www.codeproject.com/Tips/868163/IValueConverter-Example-and-Usage-in-WPF |
FWIW, we're using WPF and Caliburn.Micro's MVVM framework with Units.Net in our domain model and our view models. Implementing IValueConverter is absolutely the way to go for a two way binding. Just wanted to back up Mr Larsen on this one. |
This sounds like a good solution! However, I ran into the following problem: I want the Unit to work as an application-level userdefined setting, and the ConverterParameter cannot be bound to because it is not a Dependency Property. @JKSnd how would you solve this? |
@samirem I ran into this precise issue not too long ago. My first approach was to treat the unit conversion as a concern of the view, as it is basically just a rendering detail. As such I implemented an IValueConverter an ran into the precise problem you described: ConverterParameter is not a Dependency Property and as such not blindable. To get around that you can use an IMultiValueConverter with a MultiBinding which allows you to pass the desired unit as a second binding. This works great until you try to implement the backwards conversion, at which point you will realize that you have no way to access the unit used in the forward conversion. Which is due to the fact that IMultiValueConverters are n->1 functions which means that the backward conversion must be 1->n which is impossible to do if not all information is included in that 1. For the specific case of converting units to and from the view the IMultiValueConverter approach will only work if you either only need OneWay conversion or include the unit abbreviations in your display, only then can you parse both the quantity and unit from the string for the backward conversion. I ended up putting all conversion and rendering (by which I mean converting the numerical value and converting to string) into the view model and not using any converter. |
@penenkel Thanks for your response! I also stumpled upon the problem you describe with After some more research into this topic I found the following "hack": https://stackoverflow.com/a/13778519/4351604 Using that approach, I was able to define my unit as a Dependency Property in the converter ( I hope that this trick may help you or someone else having this issue. |
Nice one, @penenkel. Personally, I'd lean toward the view-model hold onto the unit information and allowing my mapping layer to handle converting between the view-model's units and the model's units rather than using an esoteric solution to make IValueConverters work. As far as making those units user-settable and system wide, I'm sure you've already discovered the unit structs' ToStringDefaultUnit setters? You could build a little wrapper from them to detect changes and notify view models across your system, even. |
In my (pet) project, using IValueConverters for databanding works well. Then I use instances of FieldValueConverter for binding units. |
Should I close this or are you still discussing this topic? |
I believe I got my question answered. Feel free to close it. Thanks everyone for your helpful answers. |
Great :-) |
I use this library in a WPF application that I'm building.
A recurring problem is that I haven't found a good way to databind (two-way) Units to my controls.
I wonder how you guys usually solve this? #71 sounds like a promising solution
The text was updated successfully, but these errors were encountered: