我在代碼後面的文件中有一個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。
BringIntoView方法用於FrameworkElement類型的對象。我可以使用這個自定義類型的項目嗎? – Deshan 2010-08-31 12:39:22
不適用於數據綁定組合框。 – 2010-09-01 20:49:55