Search code examples
xamlwindows-phone-8converters

Can I declare more than one converter? WP8


I am using BooleanToVisibilityConvertor for manipulating visibility of TextBlock in a ListBox DataTemplate.

Here is my XAML code:

    <phone:PhoneApplicationPage.Resources>
    <Converters:BooleanToVisibilityConvertor x:Key="booleanToVisibilityConvertor"/>
    </phone:PhoneApplicationPage.Resources>

    <TextBlock Grid.Row="2" HorizontalAlignment="Right" Padding="0,0,7,0" Visibility="{Binding AverageConsumption, Converter={StaticResource booleanToVisibilityConvertor}}"> 
     <Run Text="{Binding AverageConsumption}"/>
     <Run Text="l./100 km."/>
    </TextBlock>

And the code behind:

public class BooleanToVisibilityConvertor : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        {
            if (value != null)
            {
                if (!string.IsNullOrEmpty(value.ToString()))
                {
                    return Visibility.Visible;
                }

            }
            return Visibility.Collapsed;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

This is working correct, but i need one more (reversed) convertor for another TextBlock placed on the same position (Grid.Row="2" HorizontalAlignment="Right") with static Text="Partial Refueling" so when {Binding AverageConsumption} is not null or empty first TextBlock will be vissible and second will be collapsed and vice versa. Something like this:

    class BooleanToVisibilityConvertorReversed : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        {
            if (value != null)
            {
                if (!string.IsNullOrEmpty(value.ToString()))
                {
                    return Visibility.Collapsed;
                }
            }
            return Visibility.Visible;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I have tried to declare another convertor in XAML:

    <phone:PhoneApplicationPage.Resources>
    <Converters:BooleanToVisibilityConvertor x:Key="booleanToVisibilityConvertor"/>
    <Converters:BooleanToVisibilityConvertorReversed x:Key="booleanToVisibilityConvertorReversed"/>
    </phone:PhoneApplicationPage.Resources>

But got an exception on InitializeComponent(): 'A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll' How can i solve this?


Solution

  • Seems like I've found what is your problem. Your BooleanToVisibilityConvertorReversed class is not public. Make it public and your issues should be gone.