VisualLineElementGenerator Вертикальное выравнивание по центру - PullRequest
0 голосов
/ 07 января 2020

В моем приложении я использую TextEditor от Avalon.

С помощью следующего кода я создаю 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;
    }
}

AddSoftwareDependencyScriptControl, который создается в методе ConstructElement, выглядит следующим образом:

<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, кнопки и изображения. Ничего не получалось. Уменьшение изображения также не влияет на вертикальное положение.

Что я могу сделать, чтобы установить элемент управления по центру, чтобы он был точно в одной строке с текстом позади?

1 Ответ

0 голосов
/ 08 января 2020

Я решил это сам.

Я всегда пытался изменить маржу вверху, как Margin="0,10,0,0", но решение состоит в том, чтобы сделать маржу внизу отрицательной.

Мое решение теперь: Margin="0,0,0,-18"

...