0
votes

I have combo box in wpf application. On combo box selectionChanged event i have added one more item in combo box. I have bind the combo box with wvvm architecture. On selectionchanged my list updated but combo box item not refresh. My xaml code as follow:

<ComboBox Name="cmb"  SelectionChanged="cmb_SelectionChanged"
             ItemsSource="{Binding Source={StaticResource cmbList}}" 
             SelectedValue="{Binding Listvalue}" DisplayMemberPath="ItemName" SelectedValuePath="itemName" >                    
            </ComboBox>

I have use following code for sectionChanged:

    private void cmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DropDownItem newItemsValue = new DropDownItem { ID = 1, itemName = "newValue", strID = "newValue" };
        this.objcmbList.cmbList.Add(newItemsValue);          
        (FindResource("CmbList") as ObjectDataProvider).ObjectInstance = this;
        (FindResource("CmbList") as ObjectDataProvider).Refresh();
    }

Here my list has been updated but combo box not updated. May be Error is combo box in use. so how can i refresh my combo box item on selection changed event.

1
If you want to use MVVM then don't work directly access the control in code behind. You can refer this link - c-sharpcorner.com/article/explain-combo-box-binding-in-mvvm-wpf which shows how to bind using MVVM design pattern.user1672994
I have already bind the combo box with MVVM and it work fine. My list is updated on selection change But I am facing issue with refresh the combo box item which i made seclection changed event.Cricket Infatuation
I don't understand what you are trying to do. If you're using MVVM then why are you not updating the original collection directly, that is binded to the combobox?Shivani Katukota

1 Answers

0
votes

Binding to a standard List won't automatically update in WPF. Instead of using a List use an ObservableCollection in your view model instead eg:

Public ObservableCollection<string> cmbList = New ObservableCollection<string>()

and then bind to it in your XAML:

<ComboBox Name="cmb"  SelectionChanged="cmb_SelectionChanged"
         ItemsSource="{Binding Path=cmbList}}" 
         SelectedValue="{Binding Listvalue}" DisplayMemberPath="ItemName" SelectedValuePath="itemName" >                    
        </ComboBox>