Dependency Property on ValueConverter
- by spoon16
I'm trying to initialize a converter in the Resources section of my UserControl with a reference to one of the objects in my control. When I try to run the application I get an XAML parse exception.
XAML:
<UserControl.Resources>
<converter:PointConverter x:Key="pointConverter" Map="{Binding ElementName=ThingMap}" />
</UserControl.Resources>
<Grid>
<m:Map
x:Name="ThingMap" />
</Grid>
Point Converter Class:
public class PointConverter :
DependencyObject,
IValueConverter
{
public Microsoft.Maps.MapControl.Map Map
{
get { return (Microsoft.Maps.MapControl.Map)GetValue(MapProperty); }
set { SetValue(MapProperty, value); }
}
// Using a DependencyProperty as the backing store for Map. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MapProperty =
DependencyProperty.Register("Map", typeof(Microsoft.Maps.MapControl.Map), typeof(PointConverter), null);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string param = (string)parameter;
Microsoft.Maps.MapControl.Location location = value as Microsoft.Maps.MapControl.Location;
if (location != null)
{
Point point = Map.LocationToViewportPoint(location);
if (string.Compare(param.ToUpper(), "X") == 0)
return point.X;
else if (string.Compare(param.ToUpper(), "Y") == 0)
return point.Y;
return point;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}