Search code examples
c#wpfscrollviewertextblock

How to put textblock inside scrollviewer by coding behide ? (wpf)


I have a xaml code like this

<Grid>
    <... some grid row and column definitions .../>

    <ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto">
        <TextBlock "some attribute" />
    </ScrollViewer>

</Grid>

I don't know how to tie Textblock with Scrollviewer in C#. I want to use Textblock with Scrollviewer. If you have another idea, please tell me.

Many Thanks for your help. :D


Solution

  • just use the Content Property

                var myScrollViewer = new ScrollViewer();
                myScrollViewer.Content= new TextBlock();
    

    Edit

    or in combination with XAML

    XAML

    <ScrollViewer Name="myScrollViewer " Grid.Column="1" VerticalScrollBarVisibility="Auto"/>
    

    Code behind

                myScrollViewer.Content= new TextBlock(); // or what ever you want to add :-)