KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Using MarkupExtension ValueConverters does not work

21 Apr 2021, 17:03

I tried using a standard technique of declaring a ValueConverter as a MarkupExtension for easy use.

In WPF it works fine, in Noesis however I get this error in Unity:
[noesis] Assets/Scripts/MapEditor/Views/ShellView.xaml(24): Can't set 'VacuumBreather.Montreal.MapEditor.ViewModels.EnumComparisonConverter' on property 'Binding.Converter' of type 'IValueConverter'.
This is how the converter is implemented and used:
    /// <summary>
    ///     Converts a boolean to an enum value specified as the converter parameter.
    ///     Use to bind in a group of radio buttons to an enum property.
    /// </summary>
    /// <seealso cref="MarkupExtension" />
    /// <seealso cref="IValueConverter" />
    public class EnumComparisonConverter : MarkupExtension, IValueConverter
    {
        #region Constants and Fields

        private static readonly EnumComparisonConverter Instance = new EnumComparisonConverter();

        #endregion

        #region IValueConverter Implementation

        /// <inheritdoc />
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value?.Equals(parameter);
        }

        /// <inheritdoc />
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value?.Equals(true) == true ? parameter : Binding.DoNothing;
        }

        #endregion

        #region Public Methods

        /// <inheritdoc />
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return Instance;
        }

        #endregion
    }
<RadioButton IsChecked="{Binding Path=SelectedMode,
                                                 Converter={viewModels:EnumComparisonConverter},
                                                 ConverterParameter={x:Static viewModels:PaintingMode.Terrain},
                                                 Mode=TwoWay}" />
Is this a bug?
 
KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Re: Using MarkupExtension ValueConverters does not work

21 Apr 2021, 17:08

PS: I also tried declaring this same converter as a resource and changing the usage to the standard {StaticResource #KEY#} version. However I still get the same error.
Apparently Noesis doesn't like a converter being a MarkupExtension at the same time, even if it isn't used as one. So deriving from MarkupExtension seems to be the issue.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Using MarkupExtension ValueConverters does not work

21 Apr 2021, 17:15

Yes, the problem is in Noesis we don't support extending a MarkupExtension and implementing the IValueConverter at the same time. You should just implement the IValueConverter interface and use it as a static resource:
public class EnumComparisonConverter : IValueConverter
{
    #region IValueConverter Implementation

    /// <inheritdoc />
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value?.Equals(parameter);
    }

    /// <inheritdoc />
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value?.Equals(true) == true ? parameter : Binding.DoNothing;
    }

    #endregion
}
<Grid>
  <Grid.Resources>
    <viewModels:EnumComparisonConverter x:Key="enumComparisonConverter"/>
  </Grid.Resources>
  <RadioButton IsChecked="{Binding Path=SelectedMode,
    Converter={StaticResource enumComparisonConverter},
    ConverterParameter={x:Static viewModels:PaintingMode.Terrain},
    Mode=TwoWay}" />
</Grid>
 
KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Re: Using MarkupExtension ValueConverters does not work

21 Apr 2021, 23:08

I know how to work around this, but considering what an incredibly common WPF developer technique this is, I consider this a bug.
There is no obvious reason for a developer to think that this doesn't work and it's a big inconsistency with WPF. As I said, this is a really common
technique among WPF developers.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Using MarkupExtension ValueConverters does not work

22 Apr 2021, 10:56

Any deviation from WPF we consider it a bug, I was trying to provide a solution while we solve it.
I created the corresponding ticket in our bugtracker to fix it in a future release: #1975
Thanks for the feedback.
 
KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Re: Using MarkupExtension ValueConverters does not work

23 Apr 2021, 19:46

Don't missunderstand me, I appreciate the workaround (which I was aware of). I just wanted to make sure this was actually a bug and I wasn't doing anything wrong.

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 14 guests