-1

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);
Techies
  • 29
  • 4
  • How about just creating the TextBlock object normally (`new TextBlock()` etc.)? Or perhaps use data binding/templating to have WPF create it for you? That depends on your exact scenario. – vesan Oct 07 '15 at 03:21
  • My scenario is I want push the XAML code which is like in controller and display in XAML. – Techies Oct 07 '15 at 03:31
  • Yes, I understand that from the question. Where does the XAML string come from, though? Is it possible to create your element in code instead? If not, what's wrong with your code? Does it work? Does it throw exceptions? – vesan Oct 07 '15 at 04:06
  • The XAML string is come from a XAML file. Previously I successfully read XML file. Hence now I have to push the XAML code which read from XML file in controller. – Techies Oct 07 '15 at 04:49

1 Answers1

0
        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);
Techies
  • 29
  • 4