Matthew The Terrible
Matthew The Terrible

Reputation: 1643

Converter cannot be applied to a property that expects the type System.Windows.Data.IValueConverter

I am having a problem in my WPF app that I do not quite understand. I am trying to bind a fill to a different color depending on the value of a certain property.

Here are the snippets involved:

public class GeoLocations
{

    private static ObservableCollection<Bus> _locations = new ObservableCollection<Bus>();
    public static ObservableCollection<Bus> locations
    {
        get { return _locations; }
        set 
        { 
            _locations = value;      
        }
    }
.
.
.
}
public class Bus : INotifyPropertyChanged
{
    private double _VSAI;
    public double VSAI
    {
        get
        {
            return _VSAI;
        }
        set
        {
            _VSAI = value;
            OnPropertyChanged(new PropertyChangedEventArgs("VSAI"));
        }
    }
public class VsaiToColorConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double vsai = (double)value;

        if (vsai < App.Settings.medVSAI)
            return Brushes.Green;

        if (vsai >= App.Settings.medVSAI && vsai <= App.Settings.maxVSAI)
            return Brushes.Yellow;


        if (vsai > App.Settings.maxVSAI)
            return Brushes.Red;

        return Brushes.Transparent;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Then in my xaml I have the following:

in my resources I have

<local:GeoLocations x:Key="geolocs"/>

and then I have a map

<ig:GeographicProportionalSymbolSeries x:Name="ProportionalSeries"
                                                       LongitudeMemberPath="Longitude"
                                                       LatitudeMemberPath="Latitude"
                                                       ItemsSource="{Binding Source={StaticResource geolocs}, Path=locations}"
                                                       >
                    <!-- custom marker template for GeographicProportionalSymbolSeries -->
                    <ig:GeographicProportionalSymbolSeries.MarkerTemplate>
                        <DataTemplate>
                            <Ellipse x:Name="RootElement" Width="10" Height="10" 
                                     Stroke="DarkGray"
                                     Opacity=".8"
                                     StrokeThickness="0.5" 
                                     Fill="{Binding Path=Item.VSAI, Converter={StaticResource VSAIConverter}}">
                          </Ellipse>
                        </DataTemplate>
                    </ig:GeographicProportionalSymbolSeries.MarkerTemplate>

But the error I'm getting is on the FILL=..... above. I'm hoping this is an easy fix. I'm just a little too new still to understand how to fix this and what this error means.

Upvotes: 2

Views: 3459

Answers (1)

keyboardP
keyboardP

Reputation: 69372

Your converter needs to implement the IValueConverter interface. You've implemented the two methods but you omitted the IValueConverter interface so as far as the CLR is concerned, you just have a class that happens to have the same methods as IValueConverter but isn't actually implementing it.

public class VsaiToColorConverter : IValueConverter

You generally want to handle null value cases as well

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{  
    if(value == null) return Brushes.Transparent; //or null
    double vsai = (double)value;
    //..
}

Upvotes: 7

Related Questions