none
Refresh ComboBox RRS feed

  • Question

  • I have multiple comboboxes  in a same window.

    Each combobox is filled with the item selected in previous combobox in Selection Changed event.

    For e.g  If I select an item in first combobox the second combo box is filled .

    Then If I select an item in second combobox the third combobox is filled but if I again change the item in first one the collection changes but combox does not refresh and it shows the same data.

     

     

    <ComboBox Height="23" Margin="130,28,83,0" Name="cmbEmployee" VerticalAlignment="Top" ItemsSource="{Binding Path=Employees}" DisplayMemberPath="Name" SelectedValue="{Binding Path=Name}"  SelectionChanged="cmbEmployee_SelectionChanged"/>

     

    public Collection <Employee> Employees

    {

                get;

                set;

    }

     

    I am binding the combobox as below:

    This.Employees = new Collection<Employees>(lstEmployee);

    cmbEmployee.DataContext = this;

     

    Please let me know how I can update the combobox if collection changes.

    Thanks

    Friday, May 28, 2010 6:33 PM

Answers

  • I provide sample.

    public partial class MainWindow : Window
     {
    EmployeeModel model = new EmployeeModel();

    public MainWindow()
      {
    this.InitializeComponent();

       DataContext = model;
      }

     }

    public class EmployeeModel
     {
    public EmployeeModel()
      {
       First = new ObservableCollection<Employee>();
    for (int i = 0; i < 5; i++)
       {
    var em = new Employee { Name = i.ToString() };
    for (int j = 0; j < 5; j++)
        {
        var em2 = new Employee { Name = string.Format("{0}-{1}", i, j) };

        for (int k = 0; k < 5; k++)
            {
                em2.SubEmployees.Add(new Employee { Name = string.Format("{0}-{1}-{2}", i, j, k) });
            }
            em.SubEmployees.Add(em2);
        }
        First.Add(em);

       }
      }

    public ObservableCollection<Employee> First { getprivate set; }
     }

    public class Employee
     {
    public Employee()
      {
       SubEmployees = new ObservableCollection<Employee>();
      }

    public string Name { getset; }

    public ObservableCollection<Employee> SubEmployees { getprivate set; }
     }

    and XAML:

    <Grid x:Name="LayoutRoot">
            <
    Grid.RowDefinitions>
                <
    RowDefinition Height="Auto"/>
                <
    RowDefinition Height="Auto"/>
                <
    RowDefinition Height="Auto"/>
            </
    Grid.RowDefinitions>
            <
    ComboBox SelectedIndex="0" ItemsSource="{Binding First}" DisplayMemberPath="Name" Name="firstComboBox"/>
            <
    ComboBox Grid.Row="1" SelectedItem="{Binding ElementName=firstComboBox, Path=SelectedItem.SubEmployees[0], Mode=OneWay}" ItemsSource="{Binding ElementName=firstComboBox, Path=SelectedItem.SubEmployees}" DisplayMemberPath="Name" Name="secondComboBox"/>
            <
    ComboBox Grid.Row="2" SelectedItem="{Binding ElementName=secondComboBox, Path=SelectedItem.SubEmployees[0], Mode=OneWay}" ItemsSource="{Binding ElementName=secondComboBox, Path=SelectedItem.SubEmployees}" DisplayMemberPath="Name"/>
        </
    Grid>
    • Marked as answer by CSharpAtWork Friday, May 28, 2010 9:38 PM
    Friday, May 28, 2010 9:13 PM

All replies

  • Use ObservableCollection<T> instead of Collection<T>
    Friday, May 28, 2010 6:46 PM
  • Just by changing Collection to Observable Collection , it didn't work.
    Friday, May 28, 2010 6:58 PM
  • Provide your source code.
    Friday, May 28, 2010 7:45 PM
  • I have pasted the code above the way I am binding the combobox .

    In the same code I changed it to ObservableCollection.

     

    Friday, May 28, 2010 7:50 PM
  • I am new to Wpf .I need to know  what event should be raised to update the combo box collection.

    Thanks

    Friday, May 28, 2010 8:31 PM
  • I provide sample.

    public partial class MainWindow : Window
     {
    EmployeeModel model = new EmployeeModel();

    public MainWindow()
      {
    this.InitializeComponent();

       DataContext = model;
      }

     }

    public class EmployeeModel
     {
    public EmployeeModel()
      {
       First = new ObservableCollection<Employee>();
    for (int i = 0; i < 5; i++)
       {
    var em = new Employee { Name = i.ToString() };
    for (int j = 0; j < 5; j++)
        {
        var em2 = new Employee { Name = string.Format("{0}-{1}", i, j) };

        for (int k = 0; k < 5; k++)
            {
                em2.SubEmployees.Add(new Employee { Name = string.Format("{0}-{1}-{2}", i, j, k) });
            }
            em.SubEmployees.Add(em2);
        }
        First.Add(em);

       }
      }

    public ObservableCollection<Employee> First { getprivate set; }
     }

    public class Employee
     {
    public Employee()
      {
       SubEmployees = new ObservableCollection<Employee>();
      }

    public string Name { getset; }

    public ObservableCollection<Employee> SubEmployees { getprivate set; }
     }

    and XAML:

    <Grid x:Name="LayoutRoot">
            <
    Grid.RowDefinitions>
                <
    RowDefinition Height="Auto"/>
                <
    RowDefinition Height="Auto"/>
                <
    RowDefinition Height="Auto"/>
            </
    Grid.RowDefinitions>
            <
    ComboBox SelectedIndex="0" ItemsSource="{Binding First}" DisplayMemberPath="Name" Name="firstComboBox"/>
            <
    ComboBox Grid.Row="1" SelectedItem="{Binding ElementName=firstComboBox, Path=SelectedItem.SubEmployees[0], Mode=OneWay}" ItemsSource="{Binding ElementName=firstComboBox, Path=SelectedItem.SubEmployees}" DisplayMemberPath="Name" Name="secondComboBox"/>
            <
    ComboBox Grid.Row="2" SelectedItem="{Binding ElementName=secondComboBox, Path=SelectedItem.SubEmployees[0], Mode=OneWay}" ItemsSource="{Binding ElementName=secondComboBox, Path=SelectedItem.SubEmployees}" DisplayMemberPath="Name"/>
        </
    Grid>
    • Marked as answer by CSharpAtWork Friday, May 28, 2010 9:38 PM
    Friday, May 28, 2010 9:13 PM