We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to have different ComboBox choices for one property based on value of another property of SelectedObject

I have a property grid that shows info for a class that has a property with different allowable values depending on the value of another of its properties.
I have been using Enum to show choices for several properties, but in this case I don't think I can change the choices for an Enum at runtime.
I want to avoid having to create multiple enums / properties for all the possibilities and just hiding the ones that I don't want to see.

As a simple example, if the property grid is displaying an instance of a Country class, which has a Name property and a FlagColor property, 
I want to have FlagColor show as a combobox with choices of 'Red', 'White', 'Blue'  if Name = 'USA' and choices of 'Black', 'Yellow', 'Red' if Name = 'Germany' , etc.

Do you know of a good way to do this ?


7 Replies

SK Senthil Kumaran Rajan Syncfusion Team September 17, 2018 11:50 AM UTC

Hi Steve 
 
Thank you for contacting Synfusion Support. 
 
We have prepared a sample that tries to meet your requirement. In our sample, we have dynamically changed the collection of ComboBox using SelectionChanged event as shown in the below code example: 
 
C#: 
 
(FontListComboBox.Editor as FontListComboBoxEditor).fontlistbox.SelectionChanged += MainWindow_SelectionChanged; 
 
 
 
   private void MainWindow_SelectionChanged(object sender, SelectionChangedEventArgs e) 
        { 
            if ((FontListComboBox.Editor as FontListComboBoxEditor).fontlistbox.SelectedIndex == 0) 
            { 
                (Soundrecordernew.Editor as SoundRecordEditor).soundrecorder.ItemsSource = NameCollection; 
            } 
            else 
            { 
                (Soundrecordernew.Editor as SoundRecordEditor).soundrecorder.ItemsSource = CountryCollection; 
            } 
 
        } 
 
 
Please try this solution and let us know if it is helpful. 
 
Regards 
Senthil


SM Steve McWilliams September 17, 2018 07:23 PM UTC

Thanks. I was able to adapt this solution to get the combo box choices to change depending on the type of selected object.
But, there seems to be something wrong still. It works OK when I stay on the same object and just choose a different thing from the combo box.
Then the property changes correctly.

But if I select a different object, when the choices change, it is first updating the value of the associated property on the ORIGINAL object to the transform value for the newly selected object.

I attached my code to see if you see anything wrong.
See the initialize method and the OnSelectedItemChanged(), plus the CustomEditor -- TransformEditor

Attachment: ReportRevisionComponentTreeView.xaml.cs_d1db3d53.zip


SK Senthil Kumaran Rajan Syncfusion Team September 19, 2018 10:54 AM UTC

Hi Steve, 
 
Thank you for contacting Syncfusion support. 
 
As per your code provided in the last update, we suspect that your requirement is to update the SelectedObject of the PropertyGrid when the SelectedIndex of theComboBox is changed and update the ComboBox of the other SelectedObject based on the selected Item. If so, we have prepared the sample to achieve the reported requirement. This sample can be downloaded from the below location. 
 
 
Please check this sample and let us know whether the reported requirement has been achieved. If we misunderstood the reported query, Please share us some additional details about the reported requirement, it will be helpful for us to analyze and proceed further. 
 
Please let us know if you require further assistance on this. 
 
Regards, 
Senthil 



SM Steve McWilliams September 19, 2018 07:47 PM UTC

Only in this solution, everything works correctly.
Colors chosen for each country remain chosen as you go back and forth between the countries in the list.

In my real application, when I choose another item from my treeview, after updating the propgrid.SelectedObject,  there is some dependency that triggers an update on the PRIOR item's Transform property at the time I change the combobox's list of choices for the new item.
This doesn't happen in our simple example solution.

Anyway, I figured that I needed to break the connection between the combobox and the underlying object.
I didn't know how to do that the right way exactly, but I got things to work by just creating a new Editor on my CustomEditor each time the selection changes.
You can see that in my OnSelectedItemChanged() method in the attached .cs file.

So, I have a workable solution, but you might be able to shed some light on why I might have had to do this.

Attachment: PropertyGrid_3c7cc55b.zip


SK Senthil Kumaran Rajan Syncfusion Team September 20, 2018 05:01 AM UTC

Hi Steve, 
 
Thank you for the update. 
 
We have checked  your sample and found that property grid was not refreshed after initialization the CustomEditor class. To refresh the property grid, we suggest to use RefreshPropertyGrid() method  in PropertyGrid control. From your code provided in earlier update(Updated on 17/09/2018), we suspect that you have called RefreshPropertyGrid() method in HideProperties() method and invoked the method before the initailization of CustomEditor class.  
 
On further analyzing  your code provided in last update, you have called the HideProperties() methd after the initilization of CustomEditor class so that the property grid refreshed and the code works fine as you expected.  
 
We have modified your sample and it can be downloaded from below loaction. 
 
 
Please let us know if you require further assistance. 
 
Regards, 
Senthil 



SM Steve McWilliams September 20, 2018 02:34 PM UTC

OK, Thanks for the help.
It's working OK now.


SK Senthil Kumaran Rajan Syncfusion Team September 20, 2018 04:56 PM UTC

Hi Steve, 
  
Thank you for your update. 
  
We are glad to know that, your requirement has been achieved. Please let us know if you need any further assistance, we will be happy to assist you. 
  
Regards, 
Senthil 
  


Loader.
Up arrow icon