Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
604
Combobox - changing items in runtime
posted

Hi

I have XamDataGrid fields initialized in code-behind in the FieldLayoutInitializing event handler. In the data grid I have a bound field called "Level" which is a ComboBox. The items of ComboBox can change dynamicaly in run-time according to user's choise of radio-button (outside of the data grid).

This is how I populate the ComboBox of the "Level" field in FieldLayoutInitializing event handler:

 

 

 

 

 

ComboBoxItemsProvider cbip = new ComboBoxItemsProvider

();

cbip.ItemsSource = vm.GetEnumerationForField(fmd.Name);

 

 

Style s = new Style(typeof(XamComboEditor

));

s.Setters.Add(

 

new Setter(XamComboEditor

.ItemsProviderProperty,

cbip));

field.Settings.EditorType =

 

typeof(XamComboEditor

);

Where :

vm - is a ViewModel class

GetEnumerationForField(fieldName) - returns an ObservableCollection<string> of items to populate the combobox.

Now, how can I change the items of the combobox at run-time?

Thank you

  • 69686
    Suggested Answer
    posted

    Hello,

    There are couple of ways.

    1. You can create a new style and a new provider with the new items and assign that style to that field.

    2. You can define the provider as a StaticResource so that you can easily get an instance of it later and add the new items

    3. You can store the collection that you set to the provider's ItemsSource property (the one from the GetEnumerationForField method) and add items directly in it