I tried the answer by computhoms it didn't work exactly how i wanted to.
First the KeyDown event didn't generated any results , it seems the when pressing the Enter key the KeyDown event doesn't fire and doesn't generate any results.
so instead i used PreviewKeyDown event which works.
Second it didn't Write the line number for the first line. Also for subsequent numbers it creates the number in a new line then move the cursor to another line so the effect would be like this:
some text
2
some text
3
some text
and so on.
the result i want is i want it to be like this :
1-some text.
2-some text.
3-some text.
so i modified the code given by Computhos which gave me the right results.
here is the code i used:
After Creating a TextBox in xaml and creating the PreviewKeyDown event like this
<TextBox PreviewKeyDown="LifeGoalsTextBox_PreviewKeyDown" x:Name="LifeGoalsTextBox" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" />
I went to the code behind and wrote this code:
private void LifeGoalsTextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
string txt = LifeGoalsTextBox.Text;
int lineNumber = txt.Split('\n').Length ;
string lineNumberString = lineNumber.ToString();
if (lineNumber == 0)
{
LifeGoalsTextBox.Text = LifeGoalsTextBox.Text.Insert(0, "1") + '-';
}
else
{
lineNumber++;
int lastLine = 1 + LifeGoalsTextBox.Text.LastIndexOf('\n');
LifeGoalsTextBox.Text = LifeGoalsTextBox.Text.Insert(lastLine, lineNumberString+ '-') ;
LifeGoalsTextBox.CaretIndex = LifeGoalsTextBox.Text.Length;
}
}
}