ToString vs Simple DataTemplate
-
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.
-
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.
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).
-
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).
DataTemplate is actually often used for the represantation of the entire object, arbitrarily complex (both the object and the representation).
-
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.
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
-
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.
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.
-
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
:-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.
-
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.
Both points seem to be "off" (I won't say wrong, but they are off because misleading)
Member 1033907 wrote:
- 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:
- 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.
-
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.
Not an answer to your question but a comment. You mention
Label
as a means to display a string, I'll suggest you useTextBlock
instead asLabel
is not much more than a wrapper aroundTextBlock
.Label
Only adds the ability to add a border and using an access key, both you can do with out using aLabel
. See Josh Smiths discussion aboutLabel
andTextBlock
Differences between Label and TextBlock[^] -
Not an answer to your question but a comment. You mention
Label
as a means to display a string, I'll suggest you useTextBlock
instead asLabel
is not much more than a wrapper aroundTextBlock
.Label
Only adds the ability to add a border and using an access key, both you can do with out using aLabel
. See Josh Smiths discussion aboutLabel
andTextBlock
Differences between Label and TextBlock[^]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.
-
Both points seem to be "off" (I won't say wrong, but they are off because misleading)
Member 1033907 wrote:
- 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:
- 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.
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().