标签:ica mamicode ons numbers http show round question 技术
参考文档:
https://stackoverflow.com/questions/15610940/show-linenumbers-from-the-richtextbox-in-wpf
效果:

前台:
<Grid>
        <Border BorderBrush="Gray"
                BorderThickness="1">
            <ScrollViewer VerticalScrollBarVisibility="Auto"
                          HorizontalScrollBarVisibility="Disabled">
                <DockPanel>
                    <TextBlock x:Name="LineNumberTextBlock"
                               Background="#CFCFCF"
                               Foreground="#008497" />
                    <TextBox x:Name="InfoTbx"  AcceptsReturn="True"
                               BorderThickness="1 0 0 0"
                               VerticalScrollBarVisibility="Disabled"
                               TextChanged="InfoTbx_OnTextChanged">
                    </TextBox>
                </DockPanel> 
            </ScrollViewer>
        </Border>
    </Grid>
后台:
public TextBoxWithLineNumber() { InitializeComponent(); InfoTbx.Loaded += delegate { //当加载后,行号才有效 InfoTbx_OnTextChanged(InfoTbx, null); }; } public string GetInputInfo() { return InfoTbx.Text; } public void SetIsReadOnly(bool isReadOnly) { InfoTbx.IsReadOnly = isReadOnly; } public void SetTextInfo(string txt) { InfoTbx.Text = txt; } private void InfoTbx_OnTextChanged(object sender, TextChangedEventArgs e) { var textBox = sender as TextBox; var x = string.Empty; for(var i = 0; i < textBox.LineCount && i < 2000; i++) { x += i + 1 + "\n"; } LineNumberTextBlock.Text = x; }
源码:
https://files.cnblogs.com/files/lizhijian/2020-12-23-WPF%E9%87%8CTextBox%E6%98%BE%E7%A4%BA%E8%A1%8C%E5%8F%B7.zip
标签:ica mamicode ons numbers http show round question 技术
原文地址:https://www.cnblogs.com/lizhijian/p/14179679.html