question

GuoLearn-6547 avatar image
0 Votes"
GuoLearn-6547 asked GuoLearn-6547 commented

How could we copy UWP UI element in C#?

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?




windows-wpf
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Are you developing a WPF project? I noticed that you are using Window.Resources tag in your XAML which is only used in WPF.

0 Votes 0 ·

Yes,A WPF use UWP , I made a mistake,it would be Page.Resources in UWP

0 Votes 0 ·

So that is a WPF project. I'll change the tag as WPF so that the WPF engineers could check that.

0 Votes 0 ·

Hi,@GuoLearn-6547 . Do you prefer to use uwp Textblock in wpf or set xml:space="preserve" for Textblock? If you could use wpf Textblock, you can try the following methods. Any questions, please let me know.

 <Window    xmlns:System="clr-namespace:System;assembly=mscorlib">
 <Window.Resources>
             <System:String x:Key="myText" xml:space="preserve">Total:   </System:String>
             <System:String x:Key="myText1" xml:space="preserve">   Total:   </System:String>
               <Style x:Key="mytb" TargetType="TextBlock">
             <Setter Property="Text" Value="{Binding Tag,RelativeSource={RelativeSource Self},
                           StringFormat='    {0}',
                           Mode=TwoWay,
                           UpdateSourceTrigger=PropertyChanged}" />
   </Style>
 </Window.Resources>
         <StackPanel>
             <TextBlock Width="100" Height="50" Background="AliceBlue" Text="{StaticResource myText}"/>
             <TextBlock Width="100" Height="50" Background="AliceBlue" Text="{StaticResource myText1}"/>
             <TextBlock Width="100" Height="50" Background="AliceBlue" Tag="hello" Style="{StaticResource mytb}"/>
         </StackPanel>



0 Votes 0 ·
  This is not a very broad way to  make the Textblock's xml:space="preserve",we can not control what the user  input .If they input "       " or "    No.12     "  .....

  And what i see in the WPF app is the WPF app default retains the space at the end of a string,if the string is "        " we need  set xml:space="preserve" to keep the space .

`( WPF seems have a function : public UIElement DeepCopy(UIElement element) { string shapestring = XamlWriter.Save(element); StringReader stringReader = new StringReader(shapestring); XmlTextReader xmlTextReader = new XmlTextReader(stringReader); UIElement DeepCopyobject = (UIElement)XamlReader.Load(xmlTextReader); return DeepCopyobject; } Can make a broad way to copy XAML element. Is UWP have anyway to copy XAML element like the above function?

),

But the UWP default is not retains the space at the end of a string.


 So i want to know how the UWP keep the space in the textblock element,  on  a broad way,could you help me?






0 Votes 0 ·

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered GuoLearn-6547 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you anyway.

0 Votes 0 ·