Wednesday, 21 August 2013

XAML doesn't see my IValueConverter class

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

No comments:

Post a Comment