zoukankan      html  css  js  c++  java
  • WPF里TextBox显示行号

    参考文档:

    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 + "
    ";
                }
    
                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

  • 相关阅读:
    Input 银行卡验证
    记一次坑爹的加解密问题
    C# Html Agility Pack
    记一次坑爹的 “跨域” 问题
    FindControl的使用方法
    C#如何使用异步编程
    ReportViewer中设置ServerReport.ReportServerCredentials属性的方法
    C#中常用接口介绍
    谈谈C#中的接口
    DataTable与Linq相互转换
  • 原文地址:https://www.cnblogs.com/congqiandehoulai/p/14179679.html
Copyright © 2011-2022 走看看