Converter Inheritance and Members
I've been trying to come up with a working solution for piping converters, I've been through everything and decided to just fall back to a generic approach for specific converters.
And the inherited type
This was my final stop on actually getting a converter to behave as I needed it to and I'm getting "Unknown Member" errors thrown at me for the True/False properties.
If inheritance doesn't work, Is there some accepted way to pipe converters in NoesisGUI?
Code: Select all
public class BooleanConverter<T> : IValueConverter
{
public BooleanConverter(T trueValue, T falseValue)
{
True = trueValue;
False = falseValue;
}
public T True { get; set; }
public T False { get; set; }
public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is bool && ((bool)value) ? True : False;
}
public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is T && EqualityComparer<T>.Default.Equals((T)value, True);
}
}
Code: Select all
public class BooleanToVisibilityConverter : BooleanConverter<Visibility>
{
public BooleanToVisibilityConverter() :
base(Visibility.Visible, Visibility.Collapsed)
{ }
}
If inheritance doesn't work, Is there some accepted way to pipe converters in NoesisGUI?
-
-
sfernandez
Site Admin
- Posts: 3197
- Joined:
Re: Converter Inheritance and Members
Noesis already provides a BooleanToVisibilityConverter that is available in xaml without a namespace. Could be that the error you are seeing?
If you want to have your own BooleanToVisibilityConverter then you need to define it inside a namespace and use the appropriate prefix in xaml:
If you want to have your own BooleanToVisibilityConverter then you need to define it inside a namespace and use the appropriate prefix in xaml:
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace">
<Grid.Resources>
<local:BooleanToVisibilityConverter x:Key="BooleanConverter" True="Visible" False="Collapsed"/>
</Grid.Resources>
...
</Grid>
Re: Converter Inheritance and Members
It would appear that was the problem, the namespaces were set up correctly but weren't respected in the conversion process I guess?
Changing the name of my type fixed the issue though, thanks!
Changing the name of my type fixed the issue though, thanks!
Re: Converter Inheritance and Members
Could you elaborate a bit more? It should work without changing the name (but using a custom namespace)
Re: Converter Inheritance and Members
Sure, this is how my XAML was set up...
and that gave me a "Member Not Found" error repeatedly, however now I've gone back and renamed the class once again to what it was in order to reproduce the error that seems to not be a problem anymore...
I'm more confused than anything at this stage, I have no idea what caused it to throw errors in the first place.
Code: Select all
<UserControl x:Class="UI.PlayerHud"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:converters="clr-namespace:UI.Converters"
mc:Ignorable="d"
d:DesignHeight="1080 " d:DesignWidth="1920">
<UserControl.Resources>
<converters:BooleanToVisibilityConverter x:Key="boolToVisConverter"
True="Visible"
False="Collapsed"/>
<converters:BooleanToVisibilityConverter x:Key="inverseBoolToVisConverter"
True="Collapsed"
False="Visible"/>
</UserControl.Resources>
...
</UserControl>
I'm more confused than anything at this stage, I have no idea what caused it to throw errors in the first place.
Re: Converter Inheritance and Members
The only reasonable explanation I can find (apart from a bug here, that's what I am trying to figure out) is that first time you tried you didn't use the namespace in the XAML.I'm more confused than anything at this stage, I have no idea what caused it to throw errors in the first place.
Re: Converter Inheritance and Members
That could well be it, but the error persisted even after the inclusion of the namespace - I assume that's probably a bug?The only reasonable explanation I can find (apart from a bug here, that's what I am trying to figure out) is that first time you tried you didn't use the namespace in the XAML.I'm more confused than anything at this stage, I have no idea what caused it to throw errors in the first place.
Re: Converter Inheritance and Members
It is a bug if you can reproduce it. 
I have been trying it here all the combinations and not able to find anything strange...

I have been trying it here all the combinations and not able to find anything strange...
Who is online
Users browsing this forum: Ahrefs [Bot] and 10 guests