Baseflow / XF-Material-Library

A Xamarin Forms library for implementing Material Design
https://baseflow.com
MIT License
647 stars 161 forks source link

Objects as Choices in MaterialTextInput #56

Closed fabcall closed 5 years ago

fabcall commented 5 years ago

Is your feature request related to a problem? Please describe. Possibility to use objects as "Choices" in the MaterialTextField. Nowadays, this control only support an array of strings.

Describe the solution you'd like Just like the built-in Picker solution, it would be nice if we could just set an ItemsSource attribute and ItemDisplayBinding to bind the value which should be shown.

Describe alternatives you've considered Using the built-in Picker solution.

contrix09 commented 5 years ago

Update on this feature request. I tried to implement the current Xamarin.Form.Picker logic that allows binding to a property of an item in a list, but it was hard to reverse engineer the logic.

What I was able to do is to use a converter to get the property value of the item, and put the property name in the converter parameter. So in Xaml, it would look like this:

<material:MaterialTextField Choices="{Binding Companies, Converter={StaticResource ChoiceConverter}, ConverterParameter=CompanyName}"
     HelperText="*Required"
     InputType="Choice"
     Placeholder="Company" />
fabcall commented 5 years ago

I guess it's a decent solution, but what could be done if we want to retrieve the Selected Item? Maybe I'm diverging a little from original issue, but let's say you want the MaterialTextInput to act exactly like a picker. So, we could have a OnSelectedItemEvent. Following the Companies example, we would have a SelectedCompany property on ViewModel. When user interacts and selects a choice (from the converted string array), we could dispatch an OnSelectedItemEvent). Ultimately, we could simply set SelectedCompany by SelectedIndex.

contrix09 commented 5 years ago

Hmm. Currently, if the input type is set to Choice, the control uses IMaterialDialog.SelectChoiceAsync method to get the value to display in the control's text. The dialog only supports specifying the choice by passing a collection of string parameter, and returns the string that was selected. The ChoiceConverter gets the collection of objects and only returns a collection of string values back to the control. There is no way to determine the selected object from the original collection, more so if there are multiple items that have the same, let's say, company name.

Perhaps for the mean time, I can implement your suggested solution but I will also add a command property (say ItemSelectedCommand?) counter part. I can use LINQ to determine and pass the object that was selected to the event/command.

contrix09 commented 5 years ago

Hi, I removed the ChoiceConverter implementation. You can now bind a collection of objects or string. There is a new property ChoicesDisplayName which you can set which property to show when you use a custom collection of objects.

fabcall commented 5 years ago

Hi Dustin! Nice! I will try ASAP. Thanks a lot for this awesome plugin.

contrix09 commented 5 years ago

Thanks. Package is still building, it will be visible for download ~30 mins.