none
Switching ItemsSource not leads to Combobox items update

    Question

  • Hi.
    I'am trying to fully replace combobox items list by reassigning ItemsSource to a new List. However combobox still draws "old" items. Combobox has correct ItemsCollection and ItemsSource but draws incorrect data.

    Why this happens?


    please, look at the small code piece to reproduce the problem.
    2nd combo draws incorrect data when the item is selected and then you change selection in 1st combo.


    Code Snippet

    <Window x:Class="WPFComboTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPFComboTest" Height="385" Width="424"
        >
        <Grid>
        <ComboBox Name="cmb1" Height="26" Margin="102,75,193,0"  VerticalAlignment="Top" DisplayMemberPath="DisplayName"  />
        <ComboBox Name="cmb2" Height="26" Margin="103,130,192,0" VerticalAlignment="Top" DisplayMemberPath="DisplayName"/>
      </Grid>
    </Window>





    Code Snippet

    namespace WPFComboTest
    {
        public partial class Window1 : System.Windows.Window
        {
            internal class ComboItem
            {
                private string _displayName;
                public string DisplayName
                {
                    get
                    {
                        return _displayName;
                    }
                }

                internal ComboItem( string displayName)
                {
                    _displayName = displayName;               
                }

            }

            public Window1()
            {
                InitializeComponent();

                cmb1.SelectionChanged += new SelectionChangedEventHandler(cmb1_SelectionChanged);
                cmb1.ItemsSource = GetList(100, 103);

            }

            void cmb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                cmb2.ItemsSource = GetList(200, 200 + cmb1.SelectedIndex );
            }
           
            private List<ComboItem> GetList(int lowerBound, int upperBound)
            {
                List<ComboItem> list = new List<ComboItem>();
                for (int i = lowerBound; i <= upperBound; i++)
                {
                    list.Add(new ComboItem(i.ToString()));
                }
                return list;
            }
        }
    }


    Friday, February 15, 2008 12:45 PM

Answers

  • Have you tried installing the .NET Framework 3.5 RTM to see if the problem still exists? I remember this problem has occured in .NET Framework 3.5 Beta, but from my experience, it never happens in .NET Framework 3.0 RTM.

    Thanks
    Tuesday, February 19, 2008 2:40 AM

All replies

  • The code you posted above works pretty well for me, so I guess you might have other things messed up here.
    Monday, February 18, 2008 8:26 AM

  • Hi again. Have some new details.
    This is a problem of combobox redrawing. It shows selection box with incorrect height.
    It seems it takes height from the previous opening.
    However if I press 'Down arrow' i can select item in combo, so combo contains correct list.

    Steps to reproduce:
    1. Select '100' in 1st combobox
    2. Select '200' in 2nd combobox
    3. Select '102' in 1st combobox
    4. Open 2nd combobox.

    Result: height is for 1 item
    Expected: height is for 4 items

    link to screenshot


    Details: OS Win2k3 + latest updates, .net 3.0

    Monday, February 18, 2008 3:45 PM
  • Have you tried installing the .NET Framework 3.5 RTM to see if the problem still exists? I remember this problem has occured in .NET Framework 3.5 Beta, but from my experience, it never happens in .NET Framework 3.0 RTM.

    Thanks
    Tuesday, February 19, 2008 2:40 AM
  • Thanks Marco.
    The problem has been solved as i installed the 3.5 RTM.
    I hope it really will not appear if customer has 3.0 only.

    Wednesday, February 20, 2008 1:33 PM
  • I am having this problem with 3.5RTM
    Monday, January 26, 2009 2:23 PM