【问题标题】:How to design a TextBox which shows line number int he Left or Right Side?如何设计一个在左侧或右侧显示行号的文本框?
【发布时间】:2011-02-17 13:45:42
【问题描述】:

是否有任何控件可以在左侧或右侧的文本框中显示行号?如果不是如何创建这样的控件?

【问题讨论】:

    标签: wpf


    【解决方案1】:

    显然你不是第一个遇到这种问题的人。

    所以这里有一些你可能会发现有用的链接:

    WPF RichTextBox to create editor with line numbers

    http://www.aqistar.com/

    【讨论】:

      【解决方案2】:

      对于 wpf,我通过将以下内容添加到 xaml 来完成此操作:-

          <Grid Margin="10">
              <TextBox x:Name="TextBoxLineNumbers" FontFamily="Courier New" FontSize="18" IsReadOnly="True" TextAlignment="Right" Text="" Foreground="#50000000" Background="Transparent" Margin="0" Padding="0" HorizontalAlignment="Left" Width="60" BorderThickness="1,1,0,1"/>
              <TextBox VerticalScrollBarVisibility="Visible" AcceptsReturn="True" ScrollViewer.ScrollChanged="TextBox_ScrollChanged"  x:Name="TextBoxResult" FontFamily="Courier New" FontSize="18" TextAlignment="Left" Text="" Foreground="Black" Background="Transparent" Margin="60,0,0,0" Padding="0" BorderThickness="0,1,1,1"/>
          </Grid>
      

      到 xaml.cs 文件:-

          private void SetUpLines(string data)
          {
              int noLines = string.IsNullOrEmpty(data) ? 0 : data.Split('\n').Length;
              StringBuilder sb = new StringBuilder();
              for (int i = 1; i <= noLines; i++)
              {
                  sb.AppendLine(i.ToString());
              }
      
              TextBoxLineNumbers.Text = sb.ToString();
              TextBoxResult.Text = data??"";
          }
      
          private void TextBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
          {
              TextBoxLineNumbers.ScrollToVerticalOffset(TextBoxResult.VerticalOffset);
          }
      

      调用 SetUpLines 来填充文本框。请注意,这使用固定大小的字体并且不进行换行,我不希望这样。我知道这是一个老问题,但我在其他任何地方都没有看到简单的答案。

      【讨论】:

        猜你喜欢
        • 2012-06-19
        • 2013-07-31
        • 1970-01-01
        • 2015-05-17
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 2018-08-04
        相关资源
        最近更新 更多