Search code examples
silverlighttextblock

How can a user enter a newline character in Silverlight?


I'm trying to get a screen in silverlight where the user can enter their own text and add line breaks as neccesary. The problem is that whenever they hit return inside of a text block, nothing happens. Is there some way around this?

Thanks


Solution

  • [Edit: For whoever voted my answer down -- the question was "how do you capture enter in a text block". The text block element does not have an AcceptsReturn attribute.]

    You should be able to trap for the Enter key and insert a newline character.

        private string textBuffer = "";
        private void TextBlock_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                textBuffer += Environment.NewLine;
            }
            else
            {
                textBuffer += e.Key.ToString();
            }
            Text.Text = textBuffer;
            e.Handled = true;
        }