WPF Combobox当前滚动位置不变

问题描述:

我在代码后面的文件中有一个ObservableCollection<ClassName>类型的公共属性,我已经将它绑定到了Combobox的ItemsSource属性。WPF Combobox当前滚动位置不变

<ComboBox Height="23" 
        Margin="82,34,71,0" 
        Name="comboBox1" 
        VerticalAlignment="Top" 
        ItemsSource="{Binding Path=Collection}" 
        DisplayMemberPath="Name" /> 

后我就填充窗体加载此集合,所有的项目都显示,我向下滚动到最后一个元素,并选择它。

现在,我点击一个按钮,这将添加另一个项目的集合,我想设置光标到列表的开始。为此,我尝试了以下代码,

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     Collection.Add(new TempObject() { Name = "new item" }); 
     comboBox1.SelectedIndex = -1; 
    } 

这样做并未将滚动条设置为列表的开头。我尝试清除列表并重新填充,但仍然无法使用。

帮助,请....

应用BringIntoView后:

private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      Collection.Clear(); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 
      Collection.Add(new TempObject() { Name = "testItem" }); 

      comboBox1.SelectedIndex = -1; 

      ComboBoxItem item = comboBox1.ItemContainerGenerator.ContainerFromIndex(0) 
                     as ComboBoxItem; 

      if (item != null) item.BringIntoView(); 
    } 

这将始终为ComboBoxItem项目返回null。

试试这个:

comboBox1.Items[0].BringIntoView(); 
+0

BringIntoView方法用于FrameworkElement类型的对象。我可以使用这个自定义类型的项目吗? – Deshan 2010-08-31 12:39:22

+1

不适用于数据绑定组合框。 – 2010-09-01 20:49:55

以“我想将光标移动到列表的开头”你的意思是你想组合框的选择项设置为第一项?然后将其设置为索引0,索引-1意味着没有选择。您的评论后

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    Collection.Add(new TempObject() { Name = "new item" }); 
    comboBox1.SelectedIndex = 0; 
} 

更新:由于您的组合框数据绑定您可以使用ItemContainerGenerator去的第一个项目。这只有在项目已经被渲染时才有效,即下拉列表已被打开。

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    Collection.Add(new TempObject() { Name = "new item" }); 
    comboBox1.SelectedIndex = -1; 
    ComboBoxItem item = comboBox1.ItemContainerGenerator.ContainerFromIndex(0) as ComboBoxItem; 
    if (item != null) item.BringIntoView(); 
} 

另一个更简单的方法是选择第一个项目,然后取消选择它。

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    Collection.Add(new TempObject() { Name = "new item" }); 
    comboBox1.SelectedIndex = 0; 
    comboBox1.SelectedIndex = -1; 
} 
+0

不,我不想选择任何项目。所选项目应该为空。只需要将滚动位置设置为下拉列表的开头。 – Deshan 2010-09-01 05:02:20

+0

非常感谢您迄今为止的回复。 但是使用ItemContainerGenerator可以正常工作,直到绑定列表不被重新实例化。例如,如果我重新实例化或清除并再次将项目添加到集合,ContainerFromIdex返回null。因此BringIntoView方法将不起作用。任何解决方法? – Deshan 2010-09-02 07:16:53

+0

这真的是个问题吗?如果您删除了所有项目,我希望滚动位置也可以重置,因此无需获取第一个项目即可调用BringIntoView。什么意思与reinstatiate?请向您的问题添加代码,以显示您遇到的问题。 – 2010-09-02 18:26:04