WPF里TextBox显示行号

2020-12-29 15:28

阅读:512

标签:ica   mamicode   ons   numbers   http   show   round   question   技术   

参考文档:

https://stackoverflow.com/questions/15610940/show-linenumbers-from-the-richtextbox-in-wpf

效果:

技术图片

 

 

前台:

"Gray"
                BorderThickness="1">
            "Auto"
                          HorizontalScrollBarVisibility="Disabled">

                "LineNumberTextBlock"
                               Background="#CFCFCF"
                               Foreground="#008497" />
                    "InfoTbx"  AcceptsReturn="True"
                               BorderThickness="1 0 0 0"
                               VerticalScrollBarVisibility="Disabled"
                               TextChanged="InfoTbx_OnTextChanged">
                    

 

后台:

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

WPF里TextBox显示行号

标签:ica   mamicode   ons   numbers   http   show   round   question   技术   

原文地址:https://www.cnblogs.com/lizhijian/p/14179679.html


评论


亲,登录后才可以留言!