VisualLineElementGenerator垂直居中对齐

时间:2020-01-07 15:38:04

标签: c# wpf avalonedit

在我的应用程序中,我正在使用Avalon的TextEditor。

使用以下代码,我正在创建一个工作正常的VisualLineElementGenerator

internal class SoftwareDependencyElementGenerator : VisualLineElementGenerator
{
    private static readonly Regex imageRegex = new Regex(@"<Dependencies>([ \t])*$");

    private readonly Action<object> doImportAction;

    public SoftwareDependencyElementGenerator(Action<object> doImportAction)
    {
        this.doImportAction = doImportAction;
    }

    private Match FindMatch(int startOffset)
    {
        int endOffset = CurrentContext.VisualLine.LastDocumentLine.EndOffset;
        TextDocument document = CurrentContext.Document;
        string relevantText = document.GetText(startOffset, endOffset - startOffset);
        return imageRegex.Match(relevantText);
    }

    public override int GetFirstInterestedOffset(int startOffset)
    {
        Match match = FindMatch(startOffset);
        return match.Success ? (startOffset + match.Index) : -1;
    }

    public override VisualLineElement ConstructElement(int offset)
    {
        Match match = FindMatch(offset);
        if (match.Success && match.Index == 0)
        {
            return new InlineObjectElement(0, new AddSoftwareDependencyScriptControl(doImportAction));
        }
        return null;
    }
}

在ConstructElement方法中创建的AddSoftwareDependencyScriptControl看起来像:

<UserControl x:Class="MyApplication.AddSoftwareDependencyScriptControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             Width="16" Height="16" >
    <Grid>
        <Button Name="btn" BorderBrush="Transparent" BorderThickness="0" Command="{Binding ShowSoftwareDependenciesCommand}" Width="16" Height="16">
            <Button.Content>
                <Grid>
                    <Image Width="14" Height="14" Cursor="Hand" ToolTip="Softwareabhängigkeit hinzufügen"
                           Source="pack://application:,,,/Resources;component/Graphics/Dependency.png"/>
                </Grid>
            </Button.Content>
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter Content="{TemplateBinding Content}"/>
                </ControlTemplate>
            </Button.Template>
        </Button>

    </Grid>
</UserControl>

要将SoftwareDependencyElementGererator添加到Avalon-TextEditor中,我只需使用:

SoftwareDependencyElementGenerator softwareDependencyElementGenerator = new SoftwareDependencyElementGenerator(SelectSoftwareDependency);
AvalonTextEditor.TextArea.TextView.ElementGenerators.Add(softwareDependencyElementGenerator);

一切正常。但是控件的位置不是我想要的位置。

enter image description here

如您所见。控件不在垂直中心。我只是尝试设置UserControl,Button和Image的VerticalAlignment。没事。同时缩小图像不会影响垂直位置。

我该怎么做才能将控件居中设置,使其恰好与后面的文字对齐?

1 个答案:

答案 0 :(得分:0)

我自己解决了。

我总是尝试更改顶部的边距,例如 forkJoin,但解决方案是使底部的边距为负。

我现在的解决方案是: complete