我的WPF应用中遇到了一个我不太了解的问题。我试图根据某个属性的值将填充绑定到不同的颜色。
以下是涉及的片段:
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();
}
}
然后在我的xaml中我有以下内容:
在我的资源中我有
<local:GeoLocations x:Key="geolocs"/>
然后我有一张地图
<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>
但我得到的错误是在FILL = .....上面。我希望这是一个简单的解决方案。我只是有点太新了解了解如何解决这个问题以及这个错误意味着什么。
答案 0 :(得分:7)
您的转换器需要实现IValueConverter接口。您已经实现了这两个方法,但是您省略了IValueConverter
接口,因此就CLR而言,您只有一个类恰好具有与IValueConverter
相同的方法,但实际上并未实现它
public class VsaiToColorConverter : IValueConverter
您通常也希望处理null value
个案
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;
//..
}