English 中文(简体)
带有ListView的数据绑定WPF TabControl导致滚动问题
原标题:Databound WPF TabControl with ListView is causing scrolling issues

我有一个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

有人知道怎么解决这个问题吗?

问题回答

Here is a thread pertaining to something similar which has a demo file (not sure if it works). Hope this helps you in the right direction.

http://www.eggheadcafe.com/community/aspnet/14/10043253/scrolling-in-a-listview.aspx

Object myItem = myList.Items[20]; myList.ScrollIntoView(myItem);

http://www.mvps.org/vbvision/_samples/Virtual_ListView_Demo.zip





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签