0
votes

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?

1
Why do you think that this line is a culprit?ixSci
I think the reason is here: <Converters:BooleanToVisibilityConvertorReversed x:Key="booleanToVisibilityConvertorReversed"/> and it just can't Initialize the page. I have removed the other converter and tried only with this one, but still have the same exception.Petko Igrev
And if you remove it the error will be gone? Maybe some hidden symbol is there? Try to type it anew with the IntelliSense helpixSci
I judt did, but it is still the same. I can Build it, but can't Initialize the page. Actually can i use more then one converter like this?Petko Igrev
Sure you can. It is just another resource. Nothing specialixSci

1 Answers

0
votes

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