Howie
Howie

Reputation: 2778

XAML doesn't see my IValueConverter class

I'm creating a simple WP8, but I'm having troubles hiding (changing the Visibility property) on a control.

In XAML I've added xmlns:local="clr-namespace:MyProjectName" (I've also tried with using). The XAML is then structured as follows:

<phone:PhoneApplicationPage
x:Class="MyProjectName.Pages.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProjectName"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True" Margin="0,4,0,-4" Background="#FFBD3F3F">

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}" >
    <Grid x:Name="ContentPanel" Grid.Row="1" >
        <Grid.Resources>
            <local:VisibilityFormatter x:Key="FormatConverter" />
        </Grid.Resources>

        <phone:LongListSelector Grid.Row="4">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                     <TextBlock Text="{Binding Obj}" 
                                     Visibility="{Binding ObjVisibility, 
                                     Mode=OneWay, 
                                     Converter={StaticResource FormatConverter}}" />
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>
    </Grid>
</Grid>

The problem is at the <local:...> line: The name "VisibilityFormatter" does not exist in the namespace "clr-namespace:MyProjectName".

The class is defined as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace MyProjectName
{
    public class Formatter
    {
        public class VisibilityFormatter : IValueConverter
        {
            // Retrieve the format string and use it to format the value.
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var visibility = parameter as bool?;
                return visibility.HasValue && visibility.Value ? Visibility.Visible : Visibility.Collapsed;
            }

            // No need to implement converting back on a one-way binding 
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
}

The class ObjInfo is a simple public class with two properties:

namespace MyProjectName.Models
{
    public class ObjInfo
    {
        public bool ObjVisibility { get; set; }
        public string Obj { get; set; }
    }
}

It's similar to this question, but no migrating is involved. I'm developing on WP8 from the get-go.

What am I trying to achieve? Well. I'm storing whether the control should be visible or not in that bool property. Since the XAML control's property only grokks the Visibility enum, but not bool, I need to convert it to that enum.

Upvotes: 0

Views: 1257

Answers (2)

Anton Sizikov
Anton Sizikov

Reputation: 9240

You don't have MyProjectName.VisibilityFormatter in your project, you have

MyProjectName.Formatter.VisibilityFormatter

You should remove Formatter class and leave only:

namespace MyProjectName
{
        public class VisibilityFormatter : IValueConverter
        {
            // Retrieve the format string and use it to format the value.
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var visibility = parameter as bool?;
                return visibility.HasValue && visibility.Value ? Visibility.Visible : Visibility.Collapsed;
            }

            // No need to implement converting back on a one-way binding 
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
    }

Upvotes: 1

Patrick
Patrick

Reputation: 17973

The VisibilityFormatter is an inner class of the Formatter class. You don't really need the Formatter class, just make the VisibilityFormatter a top class, and the XAML parser will find it.

Also, the general naming convention for converters is XXXConverter and not XXXFormatter, but that's no rule.

Upvotes: 6

Related Questions