Skip to content
Wiesław Šoltés edited this page Jul 24, 2020 · 1 revision

Install Packages

Create Behaviour

DocumentTextBindingBehavior.cs

using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Xaml.Interactivity;
using AvaloniaEdit;

namespace Sample.Behaviors
{
    public class DocumentTextBindingBehavior : Behavior<TextEditor>
    {
        private TextEditor _textEditor = null;

        public static readonly StyledProperty<string> TextProperty =
            AvaloniaProperty.Register<DocumentTextBindingBehavior, string>(nameof(Text));

        public string Text
        {
            get => GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }

        protected override void OnAttached()
        {
            base.OnAttached();

            if (AssociatedObject is TextEditor textEditor)
            {
                _textEditor = textEditor;
                _textEditor.TextChanged += TextChanged;
                this.GetObservable(TextProperty).Subscribe(TextPropertyChanged);
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            if (_textEditor != null)
            {
                _textEditor.TextChanged -= TextChanged;
            }
        }

        private void TextChanged(object sender, EventArgs eventArgs)
        {
            if (_textEditor != null && _textEditor.Document != null)
            {
                Text = _textEditor.Document.Text;
            }
        }

        private void TextPropertyChanged(string text)
        {
            if (_textEditor != null && _textEditor.Document != null && text != null)
            {
                var caretOffset = _textEditor.CaretOffset;
                _textEditor.Document.Text = text;
                _textEditor.CaretOffset = caretOffset;
            }
        }
    }
}

Create UserControl

SampleControl.xaml.cs

using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using AvaloniaEdit;
using AvaloniaEdit.CodeCompletion;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Highlighting;
using AvaloniaEdit.Indentation.CSharp;
using AvaloniaEdit.Rendering;

namespace Sample
{
    public class SampleControl : UserControl
    {
        private readonly TextEditor _textEditor;

        public ScriptControl()
        {
            InitializeComponent();
            _textEditor = this.FindControl<TextEditor>("textCode");
            _textEditor.ShowLineNumbers = true;
            _textEditor.TextArea.IndentationStrategy = new CSharpIndentationStrategy();
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }
    }
}

SampleControl .xaml

<UserControl x:Class="Sample.SampleControl "
             xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:i="clr-namespace:Avalonia.Xaml.Interactivity;assembly=Avalonia.Xaml.Interactivity"
             xmlns:behaviors="clr-namespace:Sample.Behaviors;assembly=Sample"
             xmlns:ae="clr-namespace:AvaloniaEdit;assembly=AvaloniaEdit">
    <Grid>
        <ae:TextEditor x:Name="textCode"
                       Margin="0"
                       Background="White"
                       Foreground="Black"
                       SyntaxHighlighting="C#"
                       HorizontalScrollBarVisibility="Auto"
                       VerticalScrollBarVisibility="Auto"
                       FontFamily="{DynamicResource CodeFontFamily}" 
                       FontWeight="Normal" 
                       FontSize="14"
                       WordWrap="False">
            <i:Interaction.Behaviors>
                <behaviors:DocumentTextBindingBehavior Text="{Binding Code, Mode=TwoWay}"/>
            </i:Interaction.Behaviors>
        </ae:TextEditor>
    </Grid>
</UserControl>
Clone this wiki locally