I want to add sometimes one, sometimes two different TextBlocks into a single grid field. It works fine if it’s only one; if I add two, however, they are not written one below the other but are written over each other so I can’t read any of them anymore.
Is there a possibility to maybe add a linebreak after the first TextBlock or do somthing else to avoid this behaviour?
My code to add the TextBlocks is:
for (int k = 0; k < verschiedeneFaecher.Count(); k++){
[...]
var fachLehrer = new TextBlock { Text = fachString, TextAlignment = TextAlignment.Left };
[...]
Grid.SetColumn(fachLehrer, j + 1);
Grid.SetRow(fachLehrer, i + 2);
Stundenplan.Children.Add(fachLehrer);
}
If there should be only one TextBlock, verschiedeneFaecher.Count() is 1, if there should be two, its 2.
Try adding a StackPanel to handle the multi-line nature of your requirement, and then adding the textbocks to the StackPanel .
XAML
Then…
The outer grid is not really required in this solution, but I left it in for the sake of clarity. Also, your original code mentioned the variable
ibut didn’t instantiate it so I ignored it. You should have enough to go one here to work the exact placement out, though. 🙂