Skip to content

Getting abbreviation list of a quantity #354

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

Closed
YektaMirkan opened this issue Jan 4, 2018 · 4 comments
Closed

Getting abbreviation list of a quantity #354

YektaMirkan opened this issue Jan 4, 2018 · 4 comments

Comments

@YektaMirkan
Copy link

I am trying to bind abbreviation list of quatities to combobox itemsource property. But I am not able to do it. Because I couldn't find abbreviations property . In c# code I solved this problem with using LINQ. But now I need abbreviation list in xaml. So I can't use LINQ to get abbreviations from units. There is a way to achive this problem. Using converter class to convert units to abbreviations. But I need a base class otherwise I need to create a converter class for each unit. So these are the only ways that I could find.
What do you think about base class or abbreviation list?
Or do you have any other solution to get abbreviation list of quatities ?

@angularsen
Copy link
Owner

angularsen commented Jan 7, 2018

I don't fully understand your example, but here are some pointers that may help you out:

Example converter app:
https://github.com/angularsen/UnitsNet#example-creating-a-unit-converter-app

Pull request on XAML binding to units using value converter with some reflection code (PR discusses possibility of avoiding the reflection code):
#353

If you are able to achieve what you want with C#, then you should be able to do the same with XAML by using a view model property that calculates the combobox list content in C# and binding the XAML to it. I personally prefer this approach over complicated/specialized value converters.

Does this help?

@YektaMirkan
Copy link
Author

I am able to get abbreviation list in xaml but I need to create a converter class for each unit. Because there isn't any base class for units. Temperature.Units.ToList().Skip(1).Select(Temperature.GetAbbreviation).ToList()
I have solved the problem in this way. But I want to create a common converter class which can convert units to abbreviations without knowing unit type. I think this problem can only be solved by using base class otherwise I need to use reflection and it costs me a lot.

@angularsen
Copy link
Owner

Right, if I understand it correctly the only approach right now is reflection if you truly must work with the quantities in a generic manner and cannot do things like:

if (value is Length) { /* get length abbreviations */ }
else if (value is Mass) { /* get mass abbreviations */ }

It's not elegant, but it works if you have a small set of quantities you expect to be used and is safer than reflection. #353 discusses a reflection approach.

I have created an issue to try to come up with a design proposal for a base type that will help things like your example and the #353 example.

Please contribute to #371 to help move this forward.

@angularsen
Copy link
Owner

Closing this in favor of #371. Help move that forward instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants