How to create textblock in controller with XAMLReader

100 views Asked by At

I want to create a textblock in controller with XAML code. Is it possible to do with this? Any other idea?

        // Create a string
        String sb = "<TextBlock Name='NameLabel' HorizontalAlignment='Left' TextWrapping='Wrap' FontSize='37.333' FontFamily='Intel Clear Light' Opacity='0.5' Width='277' Grid.Row='4' Grid.ColumnSpan='3' Grid.Column='1' Text='Name (as per IC)' Margin='1,0,0,80' Grid.RowSpan='2'/>";

        // Create a textblock using a XamlReader
        TextBlock myTextBlock = (TextBlock)XamlReader.Load(sb.ToString());

        // Add created button to previously created container.
        splMain.Children.Add(myTextBlock);
1

There are 1 answers

0
Techies On
        I have change my code. And now I have an error of [ splMain.Children.Add(myTextBlock); Additional information: Object reference not set to an instance of an object.]
        // Create a string
        String sb = "<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Text=\"Name (as per IC)\" Margin=\"1,0,0,80\" Grid.RowSpan=\"2\"/>";

        // Create a textblock using a XamlReader
        TextBlock myTextBlock = (TextBlock)XamlReader.Load(sb.ToString());

        // Add created button to previously created container.
        splMain.Children.Add(myTextBlock);