Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning UnsetValue in IValueConverter.ConvertBack is reported as a binding error #16325

Open
MrJul opened this issue Jul 15, 2024 · 0 comments

Comments

@MrJul
Copy link
Member

MrJul commented Jul 15, 2024

Describe the bug

Returning UnsetValue in IValueConverter.ConvertBack is reported as a binding error after the binding system refactor (#13970) in 11.1.0-rc2.

System.InvalidCastException: Could not convert '(unset)' (Avalonia.UnsetValueType) to System.Object.

In 11.0, no errors happen.

To Reproduce

View:

<Window
  xmlns="https://github.com/avaloniaui"
  xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  xmlns:local="clr-namespace:Sandbox"
  x:Class="Sandbox.MainWindow"
  x:DataType="local:MainViewModel">

  <Window.Resources>
    <local:SomeConverter x:Key="SomeConverter" />
  </Window.Resources>

  <Window.DataContext>
    <local:MainViewModel />
  </Window.DataContext>

  <TextBox Text="{Binding Value, Converter={StaticResource SomeConverter}}" />

</Window>

VM:

#nullable enable

namespace Sandbox;

public class MainViewModel
{
    public object? Value { get; set; }
}

Converter:

#nullable enable

using System;
using System.Globalization;
using Avalonia;
using Avalonia.Data.Converters;

namespace Sandbox;

public sealed class SomeConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
        => value;

    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
        => AvaloniaProperty.UnsetValue;
}

Type anything in the textbox.

Expected behavior

No errors are reported.

Avalonia version

11.1.0-rc2, master 376a841

OS

No response

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant