【问题标题】:Converter cannot be applied to a property that expects the type System.Windows.Data.IValueConverter转换器不能应用于需要 System.Windows.Data.IValueConverter 类型的属性
【发布时间】:2013-07-03 18:01:56
【问题描述】:

我在我的 WPF 应用程序中遇到了一个我不太了解的问题。我正在尝试根据某个属性的值将填充绑定到不同的颜色。

这里是涉及的sn-ps:

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=..... 上。我希望这是一个简单的解决方法。我还不太了解如何解决这个问题以及这个错误意味着什么。

【问题讨论】:

    标签: c# wpf data-binding infragistics


    【解决方案1】:

    您的转换器需要实现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;
        //..
    }
    

    【讨论】:

    • 我不知道我怎么没看到。请杀了我。像魅力一样工作。
    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    相关资源
    最近更新 更多