Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. ToString vs Simple DataTemplate

ToString vs Simple DataTemplate

Scheduled Pinned Locked Moved WPF
visual-studioquestion
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Say you have a simple object with an Enum and Display name and this object is instantiated a few times (maybe a bunch... doesn't really matter) and popped into a collection for a combo box to bind to. Which do you prefer and why? Override the ToString method to display the DisplayName or set up a simple DataTemplate that shows the DisplayName is some content (e.g. Label, TextBox->Text etc.)

    Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

    S K M S 4 Replies Last reply
    0
    • L Lost User

      Say you have a simple object with an Enum and Display name and this object is instantiated a few times (maybe a bunch... doesn't really matter) and popped into a collection for a combo box to bind to. Which do you prefer and why? Override the ToString method to display the DisplayName or set up a simple DataTemplate that shows the DisplayName is some content (e.g. Label, TextBox->Text etc.)

      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      I almost never use ToString(), but when I do, its for a representation of the entire object. The DataTemplate is binding to a single property (or multiple ones with a value converter).

      M 1 Reply Last reply
      0
      • S SledgeHammer01

        I almost never use ToString(), but when I do, its for a representation of the entire object. The DataTemplate is binding to a single property (or multiple ones with a value converter).

        M Offline
        M Offline
        Member 1033907
        wrote on last edited by
        #3

        DataTemplate is actually often used for the represantation of the entire object, arbitrarily complex (both the object and the representation).

        1 Reply Last reply
        0
        • L Lost User

          Say you have a simple object with an Enum and Display name and this object is instantiated a few times (maybe a bunch... doesn't really matter) and popped into a collection for a combo box to bind to. Which do you prefer and why? Override the ToString method to display the DisplayName or set up a simple DataTemplate that shows the DisplayName is some content (e.g. Label, TextBox->Text etc.)

          Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

          K Offline
          K Offline
          Kim Breugelmans
          wrote on last edited by
          #4

          If I only need to show 1 value in my combobox I normally just set the DisplayMemberPath of the combobox to the property I want to show.

          It is said that your life flashes before your eyes just before you die. That is true, it's called Life. - Terry Pratchett

          L 1 Reply Last reply
          0
          • L Lost User

            Say you have a simple object with an Enum and Display name and this object is instantiated a few times (maybe a bunch... doesn't really matter) and popped into a collection for a combo box to bind to. Which do you prefer and why? Override the ToString method to display the DisplayName or set up a simple DataTemplate that shows the DisplayName is some content (e.g. Label, TextBox->Text etc.)

            Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

            M Offline
            M Offline
            Member 1033907
            wrote on last edited by
            #5

            I would say that ToString "converts" the whole object to a string, simple as that. Which means 1) you can't return any fancy colorful represantation other than a simple string and 2) the ToString() is global application-wise, meaning that you can't make two comboboxes displaying the same list of items make display slightly different things (which you can do with DataTamplates). DataTemplates are much more versatile, can be specific for a particular control (combobox) or used globally. They can also display much more complex data than just a string. Then there are value converters... The call is yours, ToString may be just fine for simple combobox item represatation, setting ItemTemplate doesn't hurt either.

            L 1 Reply Last reply
            0
            • K Kim Breugelmans

              If I only need to show 1 value in my combobox I normally just set the DisplayMemberPath of the combobox to the property I want to show.

              It is said that your life flashes before your eyes just before you die. That is true, it's called Life. - Terry Pratchett

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              :-D I always forget about that attribute. Thank you. I think that is the most appropriate thing to do in these circumstances (hopefully I can engrain it in my head so I remember next time)

              Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

              1 Reply Last reply
              0
              • M Member 1033907

                I would say that ToString "converts" the whole object to a string, simple as that. Which means 1) you can't return any fancy colorful represantation other than a simple string and 2) the ToString() is global application-wise, meaning that you can't make two comboboxes displaying the same list of items make display slightly different things (which you can do with DataTamplates). DataTemplates are much more versatile, can be specific for a particular control (combobox) or used globally. They can also display much more complex data than just a string. Then there are value converters... The call is yours, ToString may be just fine for simple combobox item represatation, setting ItemTemplate doesn't hurt either.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Both points seem to be "off" (I won't say wrong, but they are off because misleading)

                Member 1033907 wrote:

                1. you can't return any fancy colorful represantation other than a simple string

                I have never seen a VM return a fancy string. VMs have nothing to do with rendering (nor should they). If you want colorful display style that specific CB. Just because an object overrides the ToString does not mean you can not style its display...

                Member 1033907 wrote:

                1. the ToString() is global application-wise, meaning that you can't make two comboboxes displaying the same list of items make display slightly different things (which you can do with DataTamplates).

                I take back my earlier point of not being wrong. This is just plain wrong. Just because ToString is global does not mean you can't change the look of it somewhere else. You can still use a DataTemplate just as you can use 10 different data templates to represent the same object (depends on which one you have referenced or "pulled in"). Think of it this way. Local resources versus having it in a resource dictionary for other controls to access. If you build the Template locally it will supercede the RD... Or you could simply reference a different RD. I understand DataTemplate are much more versatile. My question is about when you don't need anything other then the DisplayName (or a single prop to get rendered). So to state use a DataTemplate because it is more versatile is like telling a hunter to by an AK-47. Sometimes a simple spear is all you need :) Was kind of fishing for performance, best practices etc.

                Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                M 1 Reply Last reply
                0
                • L Lost User

                  Say you have a simple object with an Enum and Display name and this object is instantiated a few times (maybe a bunch... doesn't really matter) and popped into a collection for a combo box to bind to. Which do you prefer and why? Override the ToString method to display the DisplayName or set up a simple DataTemplate that shows the DisplayName is some content (e.g. Label, TextBox->Text etc.)

                  Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                  S Offline
                  S Offline
                  Simon Bang Terkildsen
                  wrote on last edited by
                  #8

                  Not an answer to your question but a comment. You mention Label as a means to display a string, I'll suggest you use TextBlock instead as Label is not much more than a wrapper around TextBlock. Label Only adds the ability to add a border and using an access key, both you can do with out using a Label. See Josh Smiths discussion about Label and TextBlock Differences between Label and TextBlock[^]

                  L 1 Reply Last reply
                  0
                  • S Simon Bang Terkildsen

                    Not an answer to your question but a comment. You mention Label as a means to display a string, I'll suggest you use TextBlock instead as Label is not much more than a wrapper around TextBlock. Label Only adds the ability to add a border and using an access key, both you can do with out using a Label. See Josh Smiths discussion about Label and TextBlock Differences between Label and TextBlock[^]

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    I mentioned label as it is a simple object with Content to be set (and can be set to anything for that matter), no other reason. But thank you for your point.

                    Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                    1 Reply Last reply
                    0
                    • L Lost User

                      Both points seem to be "off" (I won't say wrong, but they are off because misleading)

                      Member 1033907 wrote:

                      1. you can't return any fancy colorful represantation other than a simple string

                      I have never seen a VM return a fancy string. VMs have nothing to do with rendering (nor should they). If you want colorful display style that specific CB. Just because an object overrides the ToString does not mean you can not style its display...

                      Member 1033907 wrote:

                      1. the ToString() is global application-wise, meaning that you can't make two comboboxes displaying the same list of items make display slightly different things (which you can do with DataTamplates).

                      I take back my earlier point of not being wrong. This is just plain wrong. Just because ToString is global does not mean you can't change the look of it somewhere else. You can still use a DataTemplate just as you can use 10 different data templates to represent the same object (depends on which one you have referenced or "pulled in"). Think of it this way. Local resources versus having it in a resource dictionary for other controls to access. If you build the Template locally it will supercede the RD... Or you could simply reference a different RD. I understand DataTemplate are much more versatile. My question is about when you don't need anything other then the DisplayName (or a single prop to get rendered). So to state use a DataTemplate because it is more versatile is like telling a hunter to by an AK-47. Sometimes a simple spear is all you need :) Was kind of fishing for performance, best practices etc.

                      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                      M Offline
                      M Offline
                      Member 1033907
                      wrote on last edited by
                      #10

                      I wasn't wrong, just not really answering your question, sorry for that. I just wanted to outline the main differences between the two approaches, which is not really what you asked. Ad 1) sure, VM does not return colors, what I had in mind was a rather more complex object than just DisplayName and then ToString() forces you to condense all the data to one string, which can be very limiting and styling is of no help. Ad 2) of course you can use DataTemplates but I was talking about limitations of ToString().

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups