Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 978455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:03:37+00:00 2026-05-16T04:03:37+00:00

I’m trying to apply the MVVM design pattern to a diagramming application. In this

  • 0

I’m trying to apply the MVVM design pattern to a diagramming application. In this application there are different items (for example a rectangle, a circle,…). I would like to save the item type as an enum in my model.

In my modelview I made a class for every item type (rectangleViewMode, circleViewMode,…).

On my view I apply a data template to the type, so it renders like a circle, or like a rectangle.

The problem is…how can I convert the enum in my model to the requiered xxxViewMode? I have a lot of types and I would like an automatic conversion.

I’m new to MVVM and maybe there is a better approach…so better solutions are welcome! 🙂

Thank you very much

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:03:38+00:00Added an answer on May 16, 2026 at 4:03 am

    I read your question a little differently to the other answerers, i don’t believe you are just looking for a way to bind an enum to a combo, i think you are looking for a way to relate an enum value to an object type. If i got this wrong then stop reading now 🙂

    First up: I’m not sure that saving the shape types as an enumeration (or even relating the shape to an enumeration) is very scalable. Read on, and i’ll explain towards the end.

    To relate an item type to an enum, just have the item return the appropriate enum value via a property:

    public CircleViewMode
    {
        public ShapeType Shape { get { return ShapeType.Circle; }}
    }
    
    public enum ShapeType 
    {
        Circle,
        Square,
        Rectangle,
        Triangle,
        FancyShape1,
        FancyShape2
    }
    

    This means that you don’t have to employ a converter or another translator mechanism. If you want to then populate a bunch of these into a combo then it is quite simple – check this following sample and insert breakpoints at the appropriate spot to see how it works.

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                new Example().Run();
    
                Console.ReadKey();
            }
        }
    
        public class Example : INotifyPropertyChanged
        {
            public void Run()
            {
                var availableShapes = AllMyShapes.Where(x => x.NumberOfSides == 4);
                AvailableShapes = new List<KeyValuePair<string, Type>>
                    (from Shape s in availableShapes
                     select new KeyValuePair<string, Type>(
                                                            Enum.GetName(typeof(ShapeType), s.ShapeType)
                                                           ,s.GetType()
                                                           ));
    
                //at this point any combobox you have bound to the AvailableShapes property will now carry a new list of shapes
            }
    
            public List<Shape> AllMyShapes
            {
                get
                {
                    return new List<Shape>() {   new Circle(){NumberOfSides=1, ShapeType=ShapeType.Circle}
                                                ,new Square(){NumberOfSides=4, ShapeType=ShapeType.Square}
                                                ,new Rectangle(){NumberOfSides=4, ShapeType=ShapeType.Rectangle}
                                                ,new Triangle(){NumberOfSides=3, ShapeType=ShapeType.Triangle}
                                                ,new FancyShape1(){NumberOfSides=10, ShapeType=ShapeType.FancyShape1}
                                                ,new FancyShape2(){NumberOfSides=30, ShapeType=ShapeType.FancyShape2}
                                            };
                }
            }
    
            public List<KeyValuePair<string, Type>> AvailableShapes
            {
                get { return _availableShapes; }
                protected set 
                {
                    _availableShapes = value;
                }
            }
    
            protected void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private List<KeyValuePair<string, Type>> _availableShapes;
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
    
        public abstract class Shape
        {
            public int NumberOfSides { get; set; }
            public ShapeType ShapeType { get; set; }
        }
    
        public class Square : Shape { }
        public class Rectangle : Shape { }
        public class Triangle : Shape { }
        public class Circle : Shape { }
        public class FancyShape1 : Shape { }
        public class FancyShape2 : Shape { }
    
        public enum ShapeType
        {
            Circle,
            Square,
            Rectangle,
            Triangle,
            FancyShape1,
            FancyShape2
        }
    }
    

    With this approach you will have a combobox with nice human readable shape names in it, and you can instantly get the actual shape type of the selected item. It would be a trivial task to turn the class Example into an abstract base ViewModel, any ViewModel you then derive from it will have the AvailableShapes property.

    But back to my original point of scalability – as you increase the shape types you also need to update the enumeration. This could be problematic if you ship libraries of new shapes or allow users to create their own. You may be better off saving it as myShape.GetType().ToString(), which returns a string value that can then be used to recreate the an instance of the object using reflection. In the above example of showing the items in a combo, you could just get a List<Type> of the available shapes and use a converter to produce a nice human readable name from the shape type (using a string resource file, eliminating the enumeration altogether).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.