0

我想要一个带有行号的文本框。所以我决定用一个小的TextBox作为左边的行号,用另一个大的TextBox作为文本。我现在的问题是我将这两个 TextBoxes 放入 Dockpanel 中,我需要一个高度差,因为行号的 TextBox 不应该有滚动条。所以我需要缩短左边的TextBox。我的计划是在左侧 TextBox 下方放置一个空的 StackPanel。而且我遇到了麻烦,因为 DockPanel 没有按我想要的方式对我的控件进行排序。我得到它的唯一方法是使用固定宽度,但我不想要那个!

图片

还是我应该采取完全不同的方式?

4

3 回答 3

1

我不知道你为什么要构建这个控件,但你可以找到类似的 WPF 控件。请参阅此链接AvalonEdit。这是一个文本编辑器控件。

于 2012-03-20T12:59:51.980 回答
0

如果您不希望控件上有滚动条,只需将 VerticalScrollBarVisibility 设置为禁用即可。

但我不确定这正是你所需要的。如果你这样做,那么显然你的行号不会随着你的文本框滚动。您最好的选择可能是将您的两个文本框(尽管如果行号不应该是可编辑的,您可能想要使用标签代替)在停靠面板中,并将停靠面板包装在滚动查看器中。

于 2012-03-20T12:42:05.180 回答
0

You may try to use ScrollView. The code below demonstrates the idea. But I haven't come up with a solution to enable horizontal scrolling.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ScrollViewer Height="100">
            <DockPanel>
                <TextBlock DockPanel.Dock="Left">
                    <TextBlock.Inlines>
                        1<LineBreak/>
                        2<LineBreak/>
                        3<LineBreak/>
                        4<LineBreak/>
                        5<LineBreak/>
                        6<LineBreak/>
                        7<LineBreak/>
                        8<LineBreak/>
                        9<LineBreak/>
                        10<LineBreak/>
                        11<LineBreak/>
                        12<LineBreak/>
                        13<LineBreak/>
</TextBlock.Inlines>
                </TextBlock>
                <TextBox AcceptsReturn="True" TextWrapping="Wrap">
                    I want a TextBox with line numbers. So I decided to use one small TextBox for the line numbers on the left and another big one on the rigth for the text. My problem now is that I put these two TextBoxes into a Dockpanel and I need a Heigth difference because the TextBox for the line numbers should not have scrollbars. So I need to short the left TextBox. My plan is to put an empty StackPanel below the left TextBox. And I'm getting trouble because the DockPanel doesen't sort my controls like i want. The only way I got it was using a fix width but I don't want that!
                </TextBox>
            </DockPanel>
        </ScrollViewer>
    </Grid>
</Window>

It looks like Screenshot

于 2012-03-20T13:27:52.927 回答