tags:

views:

53

answers:

2

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.

+1  A: 

Try this:

comboBox1.Items[0].BringIntoView();
onof
BringIntoView method is for FrameworkElement type objects. Can i use this for Items which are of a custom type?
Deshan
Will not work for databound comboboxes.
Wallstreet Programmer
A: 

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;  
}
Wallstreet Programmer
No. I don't want to select any item. Selected Item should be null. Just want to set the Scroll Position to the begining of the Drop down List.
Deshan
Thank you very much for the responses so far.But using the ItemContainerGenerator works fine until the bound list is not re-instantiated. For example if i re-instantiate or clear and add the items again to the Collection, ContainerFromIdex returns null. Therefore BringIntoView method won't work. Any workaround for this?
Deshan
Is that really an issue here? If you remove all item I expect the scroll position to be reset as well so no need to get the first item to call BringIntoView on. What do you mean with reinstatiate? Please add code to your question that shows the issues you are having.
Wallstreet Programmer
Sorry for not posting the code. Please see the question, After "applying BringIntoView" section for the code related to Collection.Clear() issue.
Deshan