如何为 AvalonEdit 数字行添加右边距?

How to add a right margin to AvalonEdit number line?

我在 .Net Framework 4.8 项目中使用 AvalonEdit WPF 控件,在 Windows 10 64 位下使用 Visual Studio 2019。

我需要为行号添加右边距。为了理解我需要什么,我附上了一张不言自明的图片:

接下来是我的实际 xaml 代码:

<avalonEdit:TextEditor
    Grid.Column="2" Grid.Row="2"
    xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
    xmlns:editing="clr-namespace:ICSharpCode.AvalonEdit.Editing;assembly=ICSharpCode.AvalonEdit"
    xmlns:rendering="clr-namespace:ICSharpCode.AvalonEdit.Rendering;assembly=ICSharpCode.AvalonEdit"
    Name="TextEditor"
    FontFamily="Consolas"
    SyntaxHighlighting="C#"
    ShowLineNumbers="True"
    FontSize="10pt" Margin="0">
</avalonEdit:TextEditor>

希望有人能帮我解决这个问题,我花了两天时间尝试增加这个边距,但无法找到合适的解决方案。提前致谢。

虽然我评论中的方法可行,但我想到了一种更简单的方法。只需从源代码中复制 TextArea 的样式并修改它以在包含所有编辑器边距的 ItemsControl 右侧包含一些边距。款式在这里:

https://github.com/icsharpcode/AvalonEdit/blob/395ef8166870e2c6e1f63a7d97ac22e5e646e790/ICSharpCode.AvalonEdit/TextEditor.xaml#L42

这是一个完整的例子:

<Window x:Class="WpfApp1_SO_AvalonEdit.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
    xmlns:editing="clr-namespace:ICSharpCode.AvalonEdit.Editing;assembly=ICSharpCode.AvalonEdit"
    Title="Main Window"
    Width="800"
    Height="450">

<Window.Resources>
    <Style x:Shared="False" TargetType="{x:Type editing:TextArea}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="SelectionBrush">
            <Setter.Value>
                <SolidColorBrush Opacity="0.7" Color="#3399FF" />
            </Setter.Value>
        </Setter>
        <Setter Property="SelectionBorder">
            <Setter.Value>
                <Pen>
                    <Pen.Brush>
                        <SolidColorBrush Color="#3399FF" />
                    </Pen.Brush>
                </Pen>
            </Setter.Value>
        </Setter>
        <Setter Property="SelectionForeground">
            <Setter.Value>
                <SolidColorBrush Color="White" />
            </Setter.Value>
        </Setter>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type editing:TextArea}">
                    <DockPanel Focusable="False">
                        <ItemsControl Margin="0,0,10,0" Focusable="False" ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LeftMargins}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                        <ContentPresenter Panel.ZIndex="-1" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TextView}" Focusable="False" />
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid>
    <avalonEdit:TextEditor Name="TextEditor" FontFamily="Consolas" FontSize="10pt" ShowLineNumbers="True" />
</Grid>