WPF Combobox current scroll position does not change
I have a public property of type ObservableCollection<ClassName>
in the code behind file and i have bound this to the ItemsSource property of the Combobox.
<ComboBox Height="23"
Margin="82,34,71,0"
Name="comboBox1"
VerticalAlignment="Top"
ItemsSource="{Binding Path=Collection}"
DisplayMemberPath="Name" />
After i populate this collection on form load, all the items are shown and i scroll down to the last element and select it.
Now , i click on a button, which will add another item to the collection and i want to set the cursor to the beginning of the list. For this, i tried the following code,
private void button1_Click(object sender, RoutedEventArgs e)
{
Collection.Add(new TempObject() { Name = "new item" });
comboBox1.SelectedIndex = -1;
}
Doing this does not set the scroll bar to the beginning of the list. I tried clearing the list and populating it again but it still didn't work.
help please....
After applying 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();
}
This will always return null for the ComboBoxItem item.
尝试这个:
comboBox1.Items[0].BringIntoView();
With "i want to set the cursor to the beginning of the list" do you mean you want to set selected item of the combobox to the first item? Then set it to index 0, index -1 means no selection.
private void button1_Click(object sender, RoutedEventArgs e)
{
Collection.Add(new TempObject() { Name = "new item" });
comboBox1.SelectedIndex = 0;
}
Update after your comment: Since your combobox is databound you can use the ItemContainerGenerator to get to the first item. This will only works if the items have already been rendered, that is the dropdown list been opened.
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();
}
Another simplier way would be to to just select first item and next unselect it.
private void button1_Click(object sender, RoutedEventArgs e)
{
Collection.Add(new TempObject() { Name = "new item" });
comboBox1.SelectedIndex = 0;
comboBox1.SelectedIndex = -1;
}
链接地址: http://www.djcxy.com/p/41252.html
上一篇: Silverlight组合框