Hi,
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.