Silverlight 3 代码中的滚动文本块

发布于 2024-08-22 18:52:11 字数 712 浏览 9 评论 0原文

我需要滚动文本块文本。例如,文本块包含 700 个单词。这些单词从代码后面填充到文本块中,因为根据某些“内容”,它可能是不同的 700 个单词。此表单上也有一个文本框。用户将文本块中的单词键入到文本框中。当他们打字时,我会跟踪他们所在的文本块中的哪个单词。但是,并非文本块中的所有单词都适合文本块查看区域,因此我需要从代码后面滚动文本块。我该怎么做呢?

我正在使用 silverlight 3。

谢谢 shannon

将一些代码放入... 这是初学者的滚动查看器和文本块

            <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>

。当我将文本添加到 txtSource 中时,滚动查看器不会将其滚动条更改为所需的高度。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下雨或天晴 2024-08-29 18:52:11

将 TextBlock 放入滚动查看器中。当用户在文本框中输入文本时捕获事件。检查它对于当前捕获的单词是否有效,然后滚动 TextBlock。

这是一个例子。我只是在每次用户按下空格键时滚动,您需要验证输入的单词的有效性。

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>

以及事件“KeyUp”的代码:

        private void TextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key.ToString().ToLower() == "space")
        {
            scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + 15);
        }
    }

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);
        }
    }
靖瑶 2024-08-29 18:52:11

只是想一想,您是否考虑过使用 AutoCompleteBox 控件?

您可以将其 ItemsSource 绑定到一个 ObservableCollection,其中包含所有当前单词,并且可以根据需要添加或删除单词。

当用户在自动完成的文本框区域中键入时,匹配的单词集会出现在下拉列表中。

也许你正在做其他事情,但我想只是发布这个以防万一你试图重新发明轮子。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文