A common technique to avoid having to use a ListBox for entering multiple simple strings is to use a multi-line TextBox where each line represents a distinct value.
 image

One unfortunate Silverlight quirk that doesn’t necessarily rear its head until you start doing something like this. Your user dutifully enters text on separate lines and when your code decides to split the input on Environment.NewLine, you only get a single item in the resulting array!

When I encountered this my first thought was that Environment.NewLine must not be what I thought it was. I set a breakpoint to see if it’s was something other than “\r\n”.

image

Okay, so if Environment.NewLine is what I thought it was, is it possible that when I press Enter on the TextBox, it’s not inserting Environment.NewLine? I didn’t believe it, so I stepped through the debugger. Lo and behold, every time you press Enter in a Silverlight TextBox, only the \r character is entered. Still not believing, I wrote a simple application to test it:

image

The code is below, but what the application does is on the TextBox TextChanged event, it counts the number of carriage return (‘\r’) and new line (‘\n’) characters. When the Enter key is pressed, only the ‘\r’ count increases. If the Environment.NewLine button is pressed, both the ‘\r’ and ‘\n’ counts increase. Finally, if the normalize button is pressed, a method is called that ensures all new lines are Environment.NewLine (and the counts update to reflect the text change).

image

Long story short, if you need to accept multi-line input from a TextBox in Silverlight, be sure to normalize the input because when you write code to handle it, it may not be in the format you expect. There is a bug listed on Microsoft Connect that even describes this as an explicit design choice.

To see this in action, here is the XAML source:

 

And the XAML codebehind:

 

And the helper extension functions: