Home > front end >  How to use multiple controls to display each character in a string in WPF
How to use multiple controls to display each character in a string in WPF

Time:06-16

I'm using this code:

List<TextBlock> BackTextBlocks = new List<TextBlock>();
List<String> AStringList = new List<String>();
...
for (int i = 0; i < TotalCount; i  ){
    BackTextBlocks.Add(new TextBlock());
    BackTextBlocks[i].Text = AStringList[i];
    BackTextBlocks[i].Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
    BackTextBlocks[i].Arrange(new Rect(BackTextBlocks[i].DesiredSize));
    BackTextBlocks[i].Margin = new Thickness(i * BackTextBlocks[i].ActualWidth, 0, 0, 0);
}

It is normal to display Chinese and Japanese characters like this:enter image description here

but once English or other symbols are displayed, all the characters will be stacked together like this:enter image description here

The full effect should look like this:
enter image description here

CodePudding user response:

You should not manually create TextBlock elements. Better focus on the data and let the framework create the TextBlock elements for you by defining a corresponding DataTemplate (enter image description here

  • Related