Description
Is there an existing issue for this?
- I have searched the existing issues
Did you read the "Reporting a bug" section on Contributing file?
- I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui.Markup/blob/main/CONTRIBUTING.md#reporting-a-bug
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