我有一个WPF应用程序,它使用绑定到MyTab类集合的TabControl。每个TabItem都有一个绑定到MyTab类的Items属性的ListView。
我的问题是,在任何ListView中向上或向下滚动都会影响所有其他视图的位置。
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>
代码:
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: Run app and select the first tab
Step 2: Select second tab and scroll all the way down
Step 3: Select the first tab and watch that the ListView has scrolled down
Step 4: Select the second tab again and watch that the ListView has scrolled up
有人知道怎么解决这个问题吗?