Skip to content

Null-conditional operator in bindings doesn't behave as expected for deeply nested paths #18949

@Kryptos-FR

Description

@Kryptos-FR

Describe the bug

#18270 introduces the null-conditional operators in binding that was suggested in #17029. However it doesn't work as expected for more than two levels in a path, compared to the same feature in regular C# code.

To Reproduce

Consider this binding:

<TextBlock Text="{Binding Model?.Info.Name, TargetNullValue=Unknown}" />

In the bound viewmodel, there is a Model property that can be null, that has a non-null Info property with itself a Name property.

class ViewModel
{
    public Model Model? { get; set; }
}

class Model
{
    public Info Info { get; set; } = new();
}

class Info
{
    public string Name { get; set; } = "Anonymous";
}

Expected behavior

The expectation is that when Model is null, no binding error is reported (thanks to the null-conditional operator) and the TargetNullValue is used instead. That would be similar to writing in c# text = Model?.Info.Name ?? "Unknown";.

However, that is not what's happening. Instead, when Model is null the binding reports an error:

[Binding]An error occurred binding 'Text' to 'Model.Info.Name' at 'Info': 'Value is null.' (TextBlock #29530116)

By design, Info is never null so this message is very confusing. Then, instead of using the TargetNullValue as one would expect, the binding fails and is using the FallbackValue (if any).

Avalonia version

11.3.0

OS

Windows

Additional context

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions