Doombox
Topic Author
Posts: 4
Joined: 23 Apr 2018, 16:14

Converter Inheritance and Members

25 Apr 2018, 12:43

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.
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);
    }
}
And the inherited type
public class BooleanToVisibilityConverter : BooleanConverter<Visibility>
{
    public BooleanToVisibilityConverter() :
    base(Visibility.Visible, Visibility.Collapsed)
    { }
}
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?
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: Converter Inheritance and Members

25 Apr 2018, 13:26

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:
<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>
 
Doombox
Topic Author
Posts: 4
Joined: 23 Apr 2018, 16:14

Re: Converter Inheritance and Members

25 Apr 2018, 13:41

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!
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: Converter Inheritance and Members

25 Apr 2018, 13:46

Could you elaborate a bit more? It should work without changing the name (but using a custom namespace)
 
Doombox
Topic Author
Posts: 4
Joined: 23 Apr 2018, 16:14

Re: Converter Inheritance and Members

25 Apr 2018, 13:56

Sure, this is how my XAML was set up...
<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>
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.
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: Converter Inheritance and Members

25 Apr 2018, 14:17

I'm more confused than anything at this stage, I have no idea what caused it to throw errors in the first place.
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.
 
Doombox
Topic Author
Posts: 4
Joined: 23 Apr 2018, 16:14

Re: Converter Inheritance and Members

25 Apr 2018, 14:31

I'm more confused than anything at this stage, I have no idea what caused it to throw errors in the first place.
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.
That could well be it, but the error persisted even after the inclusion of the namespace - I assume that's probably a bug?
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: Converter Inheritance and Members

25 Apr 2018, 15:09

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...

Who is online

Users browsing this forum: samc and 0 guests