码迷,mamicode.com
首页 > Windows程序 > 详细

WPF里TextBox显示行号

时间:2020-12-29 11:29:53      阅读:0      评论:0      收藏:0      [点我收藏+]

标签: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

WPF里TextBox显示行号

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!