W
W
wolfak2014-02-16 19:39:08
Programming
wolfak, 2014-02-16 19:39:08

How to create a new TextBlock object in C# XAML?

Good evening.
Tell me how you can create a new TextBlock to display information in C # code?
I do this, I get an error:

public MainPage()
        {
            TextBlock story = new TextBlock();
            story.Text = "asdas";
            body.Children.Add(story);
            this.InitializeComponent();
        }

This also doesn't work:
public MainPage()
        {
            TextBlock story = new TextBlock();
            story.Text = "asdas";
            body.Children.Add(story);
            this.InitializeComponent();
        }
        class TextBlock
        {
            public string Text;
        }

I understand that it was possible to create a TextBlock in XAML and assign a value to it in C# code, but I need N number of TextBlocks.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexP11223, 2014-02-16
@wolfak

What's the error?
In my opinion, after InitializeComponent () this should be done otherwise you will not have a body yet.
The second code is strange, did you even understand what you were doing in it? Why do you need a TextBlock class with one Text field and how were you going to push the object of this class into the window? UI elements are not so simple, and it is not the heir of UIElement, which the Add method wants.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question