1

Closed

FeatureRequest: In PropertyGrid ability to pass or bind current object instance to the template

description

Usage scenario:
 
I have an object which have some properties and the I want to allow users to make selections in other properties based on what they selected other property.
 
For example:
In one property "filename" I select a CSV file and then as soon as user select the file name I want to populate comboxes for other properties "firstname", "lastname" with column names read from CSV file, which could be for example "fname", "lname".
So later user could select which columns maps to what property in my object.
 
May be it can be achieved some other way, but I thought if we can pass an object value to the template we could simple bind it to the property of the object, lets say called "columnnames", that has all column names read from csv file and bind it to ComboBox.
Closed Oct 4, 2011 at 7:39 PM by brianlagunas
BY the way, this will only work in the current source code. This will NOT work in the release version 1.5

comments

brianlagunas wrote Oct 4, 2011 at 7:38 PM

This is already possible. Start by creating a colleciton that will represent you List of options. Then define an EditorDefinition for the Column property:
            <extToolkit:EditorDefinition>
                <extToolkit:EditorDefinition.Properties>
                    <sys:String>Column</sys:String>
                </extToolkit:EditorDefinition.Properties>
                <extToolkit:EditorDefinition.EditorTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path to your Options ItemsSource}"
                                  SelectedValue="{Binding Value}" />
                    </DataTemplate>
                </extToolkit:EditorDefinition.EditorTemplate>
            </extToolkit:EditorDefinition>
        </extToolkit:PropertyGrid.EditorDefinitions>
Then when your FileName property changes simply set the Options collection to the correct items:
    private string _fileName;
    public string FileName
    {
        get { return _fileName; }
        set
        {
            _fileName = value;
            UpdateOptions(_fileName);
            OnPropertyChanged("FileName");                
        }
    }

    private void UpdateOptions(string fileName)
    {
        Options = new List<string>() { "Item 1", "Item 2", "Item 3" };
    }

    private List<string> _options;
    public List<string> Options
    {
        get { return _options; }
        set
        {
            _options = value;
            OnPropertyChanged("Options");
        }
    }
Of course, this will be slightly different depending on how you are using your Model and ViewModel. You may need to add an event handler to the PropertyChanged event of your Model (which implements the INotifyPropertyChanged interface) and check for the FileName property and load your options collection accordingly.

olegkap wrote Oct 4, 2011 at 8:18 PM

Thanks,

Yea, I am using current source code.

Thanks,
Oleg