Vignesh G
Vignesh G

Reputation: 67

Avalon Text Editor displays backslash(\) or underscore(__) when selecting special characters inside the text editor box

In my WPF application I have multiple avalon text boxes which intakes json texts. For syntax highlighting I use the official .xshd file (https://github.com/icsharpcode/AvalonEdit/blob/master/ICSharpCode.AvalonEdit/Highlighting/Resources/Json.xshd)

Issue: When I type "some, text" in my avalon textbox and when I select the comma character using double clicking the mouse(or using the keyboard shortcut to select) it displays backslash(\) automatically. Similarly if we select any other special characters like { or } it displays __.
Weird right ?

enter image description here

Since the usage of my avalon textboxes are in multiple windows I centralize the avalon text editor in a class and instantiate its object inside the xaml files wherever I need.

Below is the common class implementation by making Text property as dependency property so that i can bind the text property with my viewmodel property

public class CustomizedPrettyJSONTextBox : TextEditor, INotifyPropertyChanged
{
public CustomizedPrettyJSONTextBox()
{
    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("mynamespace.JSONSyntaxColoringRule.xshd"))
    {
        if (stream != null)
        {
            using (XmlReader reader = new XmlTextReader(stream))
            {
                IHighlightingDefinition highlightingDefinition = HighlightingLoader.Load(reader, HighlightingManager.Instance);
                ((TextEditor)this).SyntaxHighlighting = highlightingDefinition;
            }
        }
    }
}


public static DependencyProperty TextProperty =
DependencyProperty.Register("Text",
                            typeof(string), typeof(CustomizedPrettyJSONTextBox),
new PropertyMetadata((obj, args) =>
{
    CustomizedPrettyJSONTextBox target = (CustomizedPrettyJSONTextBox)obj;
    if (target.Text != (string)args.NewValue)
        target.Text = (string)args.NewValue;
})
);

public string Text
{
    get { return base.Text; }
    set { base.Text = value; }
}

protected override void OnTextChanged(EventArgs e)
{
    SetCurrentValue(TextProperty, base.Text);
    RaisePropertyChanged(TextProperty.Name);
    base.OnTextChanged(e);
}

public event PropertyChangedEventHandler? PropertyChanged;
public void RaisePropertyChanged(string info)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}
}

Below is one example of how I use this class object inside a xaml file

<mynamespace:CustomizedPrettyJSONTextBox x:Name="avalonJSONTextBox" Margin="10" Grid.Row="2"
                                         Style="{StaticResource CustomJSONTextBoxStyle}" >
    <Binding Path="MyViewModelProperty" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
        <Binding.ValidationRules>
            <commonItems:JSONValidator />
        </Binding.ValidationRules>
    </Binding>
</mynamespace:CustomizedPrettyJSONTextBox>

Please help me how to avoid this behavior.

Upvotes: 1

Views: 75

Answers (1)

Daniel
Daniel

Reputation: 16439

Try setting editor.TextArea.SelectionCornerRadius = 0; The rounded selection AvalonEdit is using doesn't work well with very thin characters, resulting in weird visuals.

Upvotes: 1

Related Questions