- KeldorKatarn
- Posts: 193
- Joined:
Using MarkupExtension ValueConverters does not work
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:
This is how the converter is implemented and used:
Is this a bug?
In WPF it works fine, in Noesis however I get this error in Unity:
Code: Select all
[noesis] Assets/Scripts/MapEditor/Views/ShellView.xaml(24): Can't set 'VacuumBreather.Montreal.MapEditor.ViewModels.EnumComparisonConverter' on property 'Binding.Converter' of type 'IValueConverter'.
Code: Select all
/// <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
}
Code: Select all
<RadioButton IsChecked="{Binding Path=SelectedMode,
Converter={viewModels:EnumComparisonConverter},
ConverterParameter={x:Static viewModels:PaintingMode.Terrain},
Mode=TwoWay}" />
- KeldorKatarn
- Posts: 193
- Joined:
Re: Using MarkupExtension ValueConverters does not work
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.
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.
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Using MarkupExtension ValueConverters does not work
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:
Code: Select all
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
}
Code: Select all
<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
- Posts: 193
- Joined:
Re: Using MarkupExtension ValueConverters does not work
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.
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.
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Using MarkupExtension ValueConverters does not work
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.
I created the corresponding ticket in our bugtracker to fix it in a future release: #1975
Thanks for the feedback.
- KeldorKatarn
- Posts: 193
- Joined:
Re: Using MarkupExtension ValueConverters does not work
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: Ahrefs [Bot], Bing [Bot], Google [Bot] and 33 guests