vpapenko / ColorPicker

Color pickers for Xamarin Forms.
MIT License
43 stars 11 forks source link

SelectedColor Binding Not Updateing ViewModel #51

Open tpitman opened 3 years ago

tpitman commented 3 years ago

Describe the bug When I try to bind the SelectedColor to a ViewModel class it gets the value, but won't set it

To Reproduce Steps to reproduce the behavior:

  1. Create a project and get ColorPicker all set up according to instructions
  2. Add using Xamarin.Forms ViewModel binding by using the BindingContext in the xaml
  3. Add a property to the ViewModel of type Xamarin.Forms.Color with a get and set
  4. Add a ColorPicker to the xaml and use standard binding format to bind it to the view model property
  5. Run the project and change the color on the color wheel

Expected behavior I would expect my view model property SET to be called any time the color changes.

Desktop (please complete the following information):

Additional context If I change my xaml to be similar to the TestApp project so that another control in the xaml uses a reference binding then it work, but only if I remove my binding to my view model property. If they are both there, then neither one updates.

Note that when I have my view model binding in place and I set a break point in the GET of my property it does hit it and retrieves the color just fine.

Here is my xaml:

<StackLayout Orientation="Vertical" BackgroundColor="{Binding Source={x:Reference LabelColorWheel}, Path=SelectedColor}"> <colorpicker:ColorCircle x:Name="LabelColorWheel" SelectedColor="{Binding LabelColor}"/> </StackLayout>

Here is my view model:

private Color _labelColor; public Color LabelColor { get { return _labelColor; } set { if (_labelColor != value) { _labelColor = value; SafePropertyChanged("LabelColor"); } } }

I am sorry my formatting isn't very good. It doesn't seem to paste code like I expect.

vpapenko commented 3 years ago

Hi. After my investigation:

BackgroundColor of StackLayout is not updated, but Text of Label with the same binding updated correctly. See example below:

<StackLayout Orientation="Vertical" BackgroundColor="{Binding Source={x:Reference LabelColorWheel}, Path=SelectedColor}">
  <colorpicker:ColorCircle x:Name="LabelColorWheel" SelectedColor="{Binding LabelColor}"/>
  <Label Text="{Binding Source={x:Reference LabelColorWheel}, Path=SelectedColor}"/>
</StackLayout>

This exact case starts working if the binding of ColorCircle is set to OneWayToSource mode:

<colorpicker:ColorCircle x:Name="LabelColorWheel" SelectedColor="{Binding LabelColor, Mode=OneWayToSource}"/>

Right now, I can't find out is it a ColorPicker problem or not. Hope it will help you.

tpitman commented 3 years ago

Thank you. I will try it out and let you know.

pasha-o commented 3 years ago

hi @vpapenko i tested onewaytosource and it works, however this is not the ideal solution, as if you change the mode to twoway and try to set the color through VM it won't work. if you also try to change the color through the wheel, it won't set the color in the view model either.

pasha-o commented 3 years ago

@vpapenko would you look into this issue and address it?

vpapenko commented 3 years ago

Hi @pasha-o I didn't found root of this problem or any solution after my brief investigations. Unfortunately, I have no time for now. So any contribution is welcome. The only possible solution I can provide now is to use new SelectedColorChanged event and code-behind.

tamnguyen98 commented 2 years ago

Binding the selected color doesnt seem to work, but the even handler for value change does

                                 SelectedColor="{Binding SelectedColor, Mode=OneWayToSource}" <!-- works if added mode-->
                                 ShowLuminosityWheel="False"
                                 SelectedColorChanged="OnColorWheelSelect"/> <!--Works-->