How could we copy UWP UI element in C#?

GuoLearn 251 Reputation points
2021-10-25T09:20:01.07+00:00

XAML: <Page.Resources>
<TextBlock xml:space="preserve" x:Key="preserveSpace"></TextBlock>
</Page.Resources>
<StackPanel name="stackPanel"/>

C#:
TextBlock textBlockStyle = this.Resources.FirstOrDefault(o=>o.Key.ToString()== "preserveSpace").Value as TextBlock;
textBlock=copy(textBlockStyle);
stackPanel.Children.Add(textBlock);
stackPanel.Children.Add(textBlock);

if we do not copy ,
stackPanel.Children.Add(textBlockStyle);
stackPanel.Children.Add(textBlockStyle);
Add textBlockStyle twice will make something error.

Could you tell me how to Copy the textBlockStyle or anyway to make all textblocks xml:space="preserve" ,please?

Developer technologies Windows Presentation Foundation
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2021-10-28T06:40:26.357+00:00

    Based on my research, UWP itself does not have such an API. You could set xml:space="preserve" to UWP textblock one by one. It is not possible to copy this property to the control and copy it. Uwp is the same as Wpf, essentially adding xml:space="preserve" to the TextBlock one by one. You can set the reserved space of TextBlock in batches by looping.


    If the answer is the right solution, please click Accept Answer and kindly upvote it. If you have extra questions about this answer, please click Comment. 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.