Dependency Property on ValueConverter

Posted by spoon16 on Stack Overflow See other posts from Stack Overflow or by spoon16
Published on 2010-03-30T05:45:49Z Indexed on 2010/03/30 5:53 UTC
Read the original article Hit count: 545

Filed under:
|
|

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();
    }
}

© Stack Overflow or respective owner

Related posts about xaml

Related posts about Silverlight