2010-08-31 68 views
0

我在代碼後面的文件中有一個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。

回答

1

試試這個:

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

BringIntoView方法用於FrameworkElement類型的對象。我可以使用這個自定義類型的項目嗎? – Deshan 2010-08-31 12:39:22

+1

不適用於數據綁定組合框。 – 2010-09-01 20:49:55

0

以「我想將光標移動到列表的開頭」你的意思是你想組合框的選擇項設置爲第一項?然後將其設置爲索引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