I'm reading Avalonia source code and I came across this sentence:
return new MenuFlyoutPresenter
{
[!ItemsControl.ItemsProperty] = this[!ItemsProperty],
[!ItemsControl.ItemTemplateProperty] = this[!ItemTemplateProperty]
};
I've never seen a syntax like that. What does those bracket do if there is no indexed property or this[] accessor?, and why are they negated with the exclamation mark if the property they are referring to is not a bool?, maybe some kind of null-check?
The code itself is contained in the following cs file:
https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/Flyouts/MenuFlyout.cs
I've tracked the code but I was unable to understand what that syntax does.
There are a couple of things going on here.
First, the syntax:
Is a collection initializer, and is shorthand for:
That indexer is defined here as:
So the
keythere is anIndexerDescriptor, and thevalueis anIBinding.So, what's going on with this thing?
We can see from your link that
ItemsPropertyis aDirectProperty<TOwner, TValue>, and that ultimately implements the!operator here:Avalonia seems to like overloading operators such as
!and~to do things you might not expect (and would normally use a method for). In this case, they use!on anAvaloniaPropertyas a shorthand for accessing that property's binding.