AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.11k stars 2.26k forks source link

Custom converter stopped working on 11.1.4 #17335

Open ffquintella opened 1 month ago

ffquintella commented 1 month ago

Describe the bug

A custom converter is being called 2 times while using on a data grid.

The first call is with the actual value and the second with a null value. This behavior started with the last upgrade.

(the converter call)

                <DataGridTextColumn IsReadOnly="True" 
                                    Header="{Binding StrFixTeam}" 
                                    Binding="{Binding FixTeamId, 
                                    Converter={StaticResource TeamIdToTeamNameConverter}, ConverterParameter=keepId}"/>

(the converter code)

public class TeamIdToTeamNameConverter: IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { //if (value is null) return "-"; if (value is null) throw new InvalidParameterException("teamId","Invalid null team id to convert");

    if (value is int sourceId && targetType.IsAssignableTo(typeof(string)))
    {
        var teamsService = GetService<ITeamsService>();

        var team = teamsService.GetById(sourceId, true);

        if (parameter != null && parameter is string variation)
        {
            if(variation == "keepId") return $"{team.Name} ({team.Value})";
        }

        return team.Name;
    }
    // converter used for the wrong type
    return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
}

public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
    //return 1;
    throw new NotSupportedException();
}

protected static T GetService<T>()
{
    var result = Locator.Current.GetService<T>();
    if (result == null) throw new Exception("Could not find service of class: " + typeof(T).Name);
    return result;
} 

}

To Reproduce

Create a datagrid with a list and a custom converter and check

Expected behavior

The converter should only be called once with the actual value. The null value is coming from no where.

Avalonia version

11.1.4

OS

No response

Additional context

No response

ffquintella commented 1 month ago

An update ... this behavior is related to the ConvertBack function. For some reason it is being called when it shouldn't

timunie commented 1 month ago

Can you try nightly?

ffquintella commented 1 month ago

There is any nupkg avaliable or a must compile ?

stevemonaco commented 1 month ago

Nightlies are available via NuGet with a custom package source (and with include prerelease checked), please check here for instructions: https://github.com/AvaloniaUI/Avalonia/wiki/Using-nightly-build-feed

Besides that, what was the exact version that worked previously for you?

ffquintella commented 1 month ago

11.1.3 worked I will get the Nightlies today and tell you

ffquintella commented 3 weeks ago

The error persists on 11.2.0 (nightly). I found a work around that is to implement the ConvertBack funtion ... but this shouldn't be needed