views:

1020

answers:

2

i need to scroll the textblock text. For example, the textblock contains 700 words. These words are populated to the textblock from code behind as it could be a different 700 words depending on some "stuff". There is a textbox on this form as well. The user types the words in the textblock into the textbox. As they type i keep track of which word from the textblock they are on. However, not all the words in the textblock will fit in the textblock viewing area, so i need to scroll the textblock from code behind. How do i go about doing this.

I"m using silverlight 3.

Thanks shannon

might be useful to put some code in... Here is the scrollviewer and text block

            <ScrollViewer x:Name="svSourceText" Width="591" MaxHeight="202" VerticalScrollBarVisibility="Auto">

                <TextBlock Height="202" Width="591"  TextWrapping="Wrap" 
                x:Name="txtSource" FontSize="12" FontFamily="Fonts/Fonts.zip#Consolas" LineHeight="21.333"
                           />

        </ScrollViewer>

for starters.. when i add text into the txtSource, the scroll viewer doesn't change it's scroll bar to be the height needed.

+1  A: 

Put the TextBlock in a scrollviewer. Capture the event when the user enters text into your TextBox. Check that it's valid for the word currently being captured and then scroll the TextBlock.

Here is an example. I'm just scrolling every time the user presses the space bar, you'd want to verify the validity of the word being entered.

XAML:

    <Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="25" />
        <RowDefinition Height="40" />
    </Grid.RowDefinitions>

    <TextBox Name="txtInput" KeyUp="TextBox_KeyUp" Width="200" Grid.Row="0" />

    <ScrollViewer Name="scrollViewer" Grid.Row="1" MaxHeight="25" MaxWidth="250" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Hidden">            
        <TextBlock>
            One
            <LineBreak />
            Two
            <LineBreak />
            Three
            <LineBreak />
            Four
            <LineBreak />
            Five
        </TextBlock>

    </ScrollViewer>


</Grid>

And the code for the event 'KeyUp':

        private void TextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key.ToString().ToLower() == "space")
        {
            scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + 15);
        }
    }
Bradley Dean
Thanks.. i did something along those lines... works better then i'd hoped for.. thanks again
jvcoach23
A: 

Just a thought, have you considered using the AutoCompleteBox control?

You could bind its ItemsSource to an ObservableCollection<string> that contains all the current words and can have words added or removed as necessary.

As the user types in the TextBox area of the AutoComplete the set of matching words appear in a drop down.

Perhaps you are doing something else but I thought just post this in case it turns out you trying to re-invent the wheel.

AnthonyWJones