AvaloniaUI / Avalonia.Samples

Avalonia.Samples aims to provide some minimal samples focusing on a particular issue at a time. This should help getting new users started.
https://www.avaloniaui.net
606 stars 103 forks source link

Advice developers to use shorter and nicer IValueConvert syntax instead of old WPF-style #51

Closed maxkatz6 closed 8 months ago

maxkatz6 commented 1 year ago

Topics to cover

Many developers don't like IValueConverters because of their verbosity. They need to create an instance of a class, implement two-way methods, add it to the resources, and so on.

Instead, we should let them know that it's possible to use converters with x:Static and FuncValueConverter. Simple examples:

public static class IntConverters
{
    public static FuncValueConverter<int, bool> IsNotZero { get; } = new(i => i != 0);
}
<Image Grid.Column="0" Source="/Assets/checkmark.png" Width="28" Height="28"
       IsVisible="{Binding Value.Count, Converter={x:Static converters:IntConverters.IsNotZero}}" />

That's it.

I haven't checked our documentation and all samples. But in general, that's what we can recommend for one-way IValueConverter and IMultiValueConverter. Additionally, we should add FuncValueConverter two-way conversion support.