1

I'm new here!

My problem is the following piece of XAML:

<ScrollViewer>
    <Label x:Name="MainText">
        <TextBlock x:Name="DefaultText">
            Application launched successfully!
        </TextBlock>
    </Label>
</ScrollViewer>
<TextBox x:Name="EntryText" KeyDown="EntryText_KeyDown" />

I want to add TextBlocks from EntryText_KeyDown inside MainText. Although it's able to access both MainText and DefaultText I don't know how I can add an element. Googling seems to provide the C# solution of MainText.Add which doesn't seem to be usable(?) in VB.

Any help would be much appreciated!

1 Answer 1

2

Label can have only one child, so if you want to have multiple textblocks use layout containers like panels, grid

<ScrollViewer>

    <StackPanel x:Name="MainText">
        <TextBlock x:Name="DefaultText">
            Application launched successfully!
        </TextBlock>
    </StackPanel >
</ScrollViewer>

Then in code behind you can do:

MainText.Children.Add(new TextBlock());

If you want to add just one textblock in label then you can do:

TextBlock textblock = new TextBlock();
textblock.Text = "My Text";
MainText.Content = textblock ;
Sign up to request clarification or add additional context in comments.

1 Comment

you will have to set the Text property like i explained in answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.