'Conditional Hide/Show in Xceed PropertyGrid

I'm using the Xceed Property Grid for my WPF application. I have a situation where a property field renders other attributes obsolete based on the selection. However the only thing I can do currently is show everything and let the user figure out what should be filled in and what should not.

Below is a good example. The [Record Matching Type] field has two options. The first option (UniqueId) should only display [Unique ID Column Name]. The rest should be hidden. How can this be done?

Example



Solution 1:[1]

This answer is very late; however, I use the Xceed property grid and have implemented what you have described.

First of all, if you do not want a property in your view model to ever display just add the [Browsable(false)] attribute above the property.

Now, if you would like properties to be able to be shown/hidden based on the selection of another property you can do the following.

  1. Add this event to your view model. public event Action<string, bool> ChangeVisible;

  2. Whenever you want a property to be shown or hidden you will raise this event. I.e. You have an "Age" property and you want to hide the "Employer" property if the Age is less than 16. In the "Age" property setter you would check the value if (value) < 16 and then raise the event ChangeVisible?.Invoke("Employer", false); To show it instead of hiding you would pass 'true' instead of 'false'.

  3. For this to work you must also add a listener on the View that hosts the view model. In the constructor of the View (let's call it 'MyView.xaml.cs') you will register a listener on the DataContext (your view model). MyViewModel.ChangeVisible += MyViewModel_ChangeVisible;

  4. In the listener you will directly call the 'ShowProperty' method of the PropertyGrid. Let's say your PropertyGrid has a name of 'MyPropertyGrid'. Your listener would look like the following.

    /// <summary>
    /// Hide/show a preference
    /// </summary>
    /// <param name="property">name of the preference</param>
    /// <param name="show">make it visible or hide it?</param>
    private void MyViewModel_ChangeVisible(string property, bool show)
    {
        Dispatcher.Invoke(() => MyPropertyGrid.ShowProperty(property, show));            
    }
    
  5. Last, but not least, the PropertyGrid does not have a ShowProperty method. So you will create a static class, with a public static extension method, so you can make all this magic work.

    public static void ShowProperty(this PropertyGrid pg, string property, bool show)
    {
        for (int i = 0; i < pg.Properties.Count; ++i)
        {
            PropertyItem prop = pg.Properties[i] as PropertyItem;
    
            if (prop.PropertyName == property)
            {
                prop.Visibility = show ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
                break;
            }
        }
    }
    

Solution 2:[2]

I haven't tested this, but you should look into using the time module. Try implementing something like this:

import time


def run_script(filePath, wait, amount):
    # Runs a file 'amount' times and waits for 'wait' seconds before running
    for _ in range(amount):
        time.sleep(wait)
        file = open(filePath, 'r')
        exec(file.read())
        file.close()


if __name__ == '__main__':
       for path in filePaths:
            proc = multiprocessing.Process(target=run_script, args=(path, wait, amount))
            procs.append(proc)
            proc.start()

        for proc in procs:
            proc.join()

Don't forget to close the file after opening it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pccoder
Solution 2 Gabe Morris