3

I am trying to create an IValueConverter that takes an enum and spits out a URI. The converter does work at runtime as expected. However the XAML Designer trows me an error saying:

Object must be the same type as the enum. The type passed in was 'Mocks.WarframeHelper_Model_Enumerations_15_1293735+RelicTypes'; the enum type was 'WarframeHelper.Model.Enumerations+RelicTypes'.

I have a simpler version of my model with the properties I only need at design time but the enum used is exactly the same (or at least should be). Is there anyway around this.

Here is the code for the IValueConverter (I am just getting the hang of those 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 data type that I use for the mock 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 designer does not like it because of the mock data.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
EnvelopedDevil
  • 658
  • 1
  • 13
  • 29
  • Do the casing match? take a look at the remarks here https://msdn.microsoft.com/en-us/library/system.enum.isdefined(v=vs.110).aspx – Nkosi Mar 25 '17 at 22:09
  • 1
    @Nkosi that is the problem, they do not match. The runtime enum is of type (Enumerations+RelicTypes) however the design time one is (Enumerations_15_1293735+RelicTypes) which is where the problem comes from. I can confirm that both the Runtime data type and the design time datatype both have a 'using Project.Model' (which is where Enumerations lives), so it should be the same type unless visual studio creates it's own data type based on the one I created – EnvelopedDevil Mar 25 '17 at 22:15
  • I was asking about the case of the value passed to the converter. For troubleshooting purposes try converting value to string before passing to `Enum.IsDefined` and see if it works. ie `Enum.IsDefined(typeof(Enumerations.RelicTypes), value.ToString())` – Nkosi Mar 25 '17 at 22:23
  • @Nkosi yep that solves it post it as an answer so I can mark it – EnvelopedDevil Mar 25 '17 at 22:38
  • @Nkosi While the designer now works and the error is gone, the expect result is not seen at design time. Is this normal behavior in this case – EnvelopedDevil Mar 25 '17 at 22:41
  • What is the current and expected result at design time. please clarify. – Nkosi Mar 25 '17 at 22:42
  • @Nkosi to send a Uri to a Image Control with a path to a local .png Image – EnvelopedDevil Mar 25 '17 at 22:49

1 Answers1

3

Converting the value to a string before passing to Enum.IsDefined it should work provided that the casing of the enums match. According to https://msdn.microsoft.com/en-us/library/system.enum.isdefined(v=vs.110).aspx

Enum.IsDefined(typeof(Enumerations.RelicTypes), value.ToString())
Nkosi
  • 235,767
  • 35
  • 427
  • 472