Display line numbers in RichEditBox UWP

I would like to know if it is possible to show the line numbers (in a separate column) of the RichEditBox in UWP C # , or if there are other ways to get it. I'm looking for a solution to this problem ... And it seems strange to me that there is no documentation on this: I only need a simple text editor. There are many Windows 10 apps out there that implement it, and I refuse to think it's impossible.

enter image description here

This is just an example from CodeWriter, a code editor application. Any idea?

+3


source to share


1 answer


I am getting the "result" thinking of the list and the RichEditBox. Now the solution is not a good one , after about 50 lines of typing it trapped like hell, but at least I tried because the question is important to me too.

So I designed ListView and RichEditBox in a 2 column grid

<ScrollViewer>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <ListView Grid.Column="0" Name="LineNumbers" ScrollViewer.VerticalScrollMode="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled"></ListView>
        <RichEditBox Grid.Column="1" x:Name="RebText" TextChanged="RebText_TextChanged" ScrollViewer.VerticalScrollMode="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled"></RichEditBox>
    </Grid>
</ScrollViewer>

      



In the code behind, I added this:

private void RebText_TextChanged(object sender, RoutedEventArgs e)
{
    //Clear line numbers
    LineNumbers.Items.Clear();
    int i = 1;

    //Get all the thext
    ITextRange text = RebText.Document.GetRange(0, TextConstants.MaxUnitCount);
    string s = text.Text;

    if (s != "\r")
    {
        //Replace return char with some char that will be never used (I hope...)
        string[] tmp = s.Replace("\r", "ยง").Split('ยง');
        foreach (string st in tmp)
        {
            //String, adding new line
            if (st != "")
            {
                LineNumbers.Items.Add(i++);
            }
            //No string, empty space
            else
            {
                LineNumbers.Items.Add("");
            }                      
        }
    }
}

      

I think the .clear () method and reading all lines every time is not good practice. But if you need a quick solution with a maximum of 50 lines, this MAYBE is the way to go.

0


source







All Articles