AvalonEdit in a DataTemplate of a TabControl

187 Views Asked by At

I'm experiencing some strange behavior of an AvalonEdit.TextEditor when placed in a TabControl item data template. I created a simple test app to illustrate as simple as I could manage: (steps to reproduce: run the app, click + twice (two tabs will appear), type something in one text editor, then switch to another tab) What I expect: seeing different text on another tab What I see: the same text I typed on the previous tab.

<Window x:Class="TestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="TestWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="80" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Button Width="80" Height="80" Grid.Row="0" Click="Button_Click">+</Button>
        <TabControl x:Name="TabSelector" ItemsSource="{Binding Tabs}" Grid.Row="1">
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <avalonEdit:TextEditor />
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>
</Window>

This is the code-behind:

using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace TestApp
{
    public partial class MainWindow: Window
    {
        public ObservableCollection<string> Tabs { get; set; } = new ObservableCollection<string>();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Tabs.Add(Guid.NewGuid().ToString());
        }
    }
}

Now when I click the + button a tab is added and the TextEditor is shown, but when I type something in and then click on another tab it shows me the same text. It is showing me the same TextEditor, but what I want is to have different TextEditors on different Tabs. When I change AvalonEdit TextEditor to a simple TextBox everything works as expected and I can type in different text on different tabs.

What might be the cause of this weirdness?

0

There are 0 best solutions below