IValueConverters and MockData

I am trying to create IValueConverter

one that takes in enum

and pulls out a URI. The converter works as expected during operation. However, the XAML Designer throws me an error:

The object must be of the same type as the enumeration. The type that was accepted was "Mocks.WarframeHelper_Model_Enumerations_15_1293735 + RelicTypes"; the enumeration type was "WarframeHelper.Model.Enumerations + RelicTypes".

I have a simpler version of my model with properties that I only need at design time, but used enum

is the same (or at least should be). Anyway, around.

Here is the code for IValueConverter

(I just get stuck on these things, so if I am doing something wrong feel free to correct me)

public class NameToUriConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if(Enum.IsDefined(typeof(Enumerations.RelicTypes), value))
        {
            return new Uri("/Assets/RelicIcons/Relic_" + (value).ToString() + ".png", UriKind.Relative);
        }
        else return new Uri("/Assets/Placeholder.png", UriKind.Relative);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value as string;
    }
}

      

and here is the custom datatype I'm using for the layout data:

public class Sample_RelicModel
{
    public Uri ImageUri { get; set; }
    public bool isVaulted { get; set; }
    public Enumerations.RelicFlavors Flavor { get; set; }
    public Enumerations.RelicTypes Type { get; set; }
    public Enumerations.DropRearity Rearity { get; set; }
    public ObservableCollection<Sample_PrimeItem_Component> DropTable { get; set; }
    private int count;
    public int Count
    {
        get { return count; }
        set
        {
            if (value >= 0)
            {
                count = value;
            }
            else MessageBox.Show("You don't have enough relics", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
    public Sample_RelicModel() { }
}

      

Again, the converter works as expected at runtime, however the XAML developer doesn't like it because of the layout data.

+3


source to share


1 answer


By converting value

to a string before going to Enum.IsDefined

, it should work as long as the enumeration wrapper matches. According to https://msdn.microsoft.com/en-us/library/system.enum.isdefined(v=vs.110).aspx



Enum.IsDefined(typeof(Enumerations.RelicTypes), value.ToString())

      

+3


source







All Articles