Skip to content

[BUG] Set nullable BindableProperty to null throws Exception #354

Open
@JoeVoo

Description

@JoeVoo

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

Setting a bindable and nullable property to null throws an exception "Unable to find target value".

Expected Behavior

should be ok
this worked in "CommunityToolkit.Maui.Markup" Version="4.2.0"

Steps To Reproduce

In my case, the problem occurs in a special control.
The control is defined in nullable environment with generic struct to let a enum picker be set to null.

public partial class EnumPicker : Picker where T : struct, Enum //allow null with struct
...

the property is defined:

public static readonly BindableProperty SelectedValueProperty =
    BindableProperty.Create(
    nameof(SelectedValue),
    typeof(T?),
    typeof(EnumPicker<T>),
    null,
    BindingMode.TwoWay,
    null,
    propertyChanged: SelectedValue_Changed);

  public T? SelectedValue
  {
      get => (T?)GetValue(SelectedValueProperty);
      **set => SetValue(SelectedValueProperty, value);**
  }

The control is used at a contenview:

     enumPicker = new EnumPicker<T>()
     {
         Margin = new Thickness(0, 5, 0, 0),
         VerticalTextAlignment = TextAlignment.Center
     }
     .Bind(EnumPicker<T>.SelectedValueProperty,
         getter: static (EnumPickerControl<T> control) => control.SelectedValue,
         setter: static (EnumPickerControl<T> control, T? value) => control.SelectedValue = value,
         source: this);

After giving the Selected Value an enum value and set it back to null, the exception will be thrown at
set => SetValue(SelectedValueProperty, value);

Link to public reproduction project repository

hope this is not needed

Environment

- .NET MAUI C# Markup CommunityToolkit: 5.1.0
- CommunityToolkit.Maui: 10.0.0
- OS: Windows 10 Build 10.0.19041.0
- .NET MAUI: 9.0.21

Anything else?

Remove the exception throw in your TypedBinding.cs, Line 249:

var value = GetTargetValue(target.GetValue(property), typeof(TProperty))/* ?? throw new InvalidOperationException("Unable to find target value")*/;

returning a null is not a fault here. For my use, after removing the exception, it works than as it should.

More testing makes no sense for me, because i don't know the intention of throwing this exception here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingunverifiedBug has not been verified by maintainers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions