WPF TabControl basic binding with ListView causes scrolling issues

I have a WPF application using TabControl associated with a collection of MyTab class. Each TabItem has a ListView associated with the Items property of the MyTab class.

My problem is that scrolling up or down in any ListView affects the position of everyone else.

XAML:

<Window>
  <Window.Resources>
    <DataTemplate x:Key="TabHeaderTemplate">
      <TextBlock Text="{Binding Header}" />
    </DataTemplate>
    <DataTemplate x:Key="TabItemTemplate">
      <ListView ItemsSource="{Binding Items}">
        <ListView.View>
          <GridView>
            <GridViewColumn Header="col1" DisplayMemberBinding="{Binding col1}" />
            <GridViewColumn Header="col2" DisplayMemberBinding="{Binding col2}" />
          </GridView>
        </ListView.View>
      </ListView>
    </DataTemplate>
  </Window.Resources>

  <Grid>
    <TabControl ItemsSource="{Binding Tabs}" ItemTemplate="{StaticResource TabHeaderTemplate}" ContentTemplate="{StaticResource TabItemTemplate}"/>
  </Grid>
</Window>

The code:

public class MyTab
{
  public string Header { get; set; }
  public ObservableCollection<MyItem> Items { get; set; }
  public MyTab(int count)
  {
    Header = count.ToString();
    Items = new ObservableCollection<MyItem>();
    for (var i = 0; i < count; i++)
      Items.Add(new MyItem {col1 = i.ToString(), col2 = i.ToString()});
  }
}

public class MyItem
{
  public string col1 { get; set; }
  public string col2 { get; set; }
}

public class MainViewModel
{
  public ObservableCollection<MyTab> Tabs { get; set; }
  public MainViewModel() { Tabs = new ObservableCollection<MyTab> {new MyTab(10), new MyTab(50)}; }
}

Step 1. Launch the application and select the first tab. Step 2. Select the second tab and scroll all the way down. Step 3: Select the first tab and see the ListView scroll down. Step 4: Select the second tab again and see the ListView scroll up.




Does anyone know how to solve this?

+3

Source: https://habr.com/ru/post/1769538/


All Articles