This project has moved. For the latest updates, please go here.

PropertyGrid Custom ItemsSource defined at Runtime

Feb 4, 2013 at 10:09 PM
I need to be able to supply the values for my custom ItemsSource at runtime, but haven't found an easy way to do it.

For now, I have only been able to do this using an IItemsSource attribute, saving all of my current items to some location on disk, and having the attribute instance load it up from that place. This has been working as a temporary solution but is not maintainable and has a ton of issues.

I submitted a Work Item here

http://wpftoolkit.codeplex.com/workitem/19208

but don't know if it's being considered, or if the functionality already exists.

Does anyone have a solution to this problem?
Feb 5, 2013 at 2:22 PM
We are working to add some flexibility to the PropertyGrid (v2.0) that will provide an alternative to the address "static" nature of using custom attributes on properties. Hopefully this may provide you solutions to your problems.
Feb 13, 2013 at 1:20 PM
Edited Feb 13, 2013 at 3:28 PM
Just in case someone else has this same problem, I'll submit here what my current workaround is until the PropertyGrid supports a more flexible way to do it.
    public static class PropertyGridHelper
    {
        public static DataTemplate GetComboBoxEditorTemplate(object itemsSource, string selectedValuePath, string displayMemberPath, string valuePath)
        {
            var dataTemplate = new DataTemplate();
            var comboBox = new FrameworkElementFactory(typeof(ComboBox));
            comboBox.SetValue(FrameworkElement.MarginProperty, new Thickness(1, 1, 1, 1));
            if (!string.IsNullOrEmpty(selectedValuePath))
            {
                comboBox.SetValue(Selector.SelectedValuePathProperty, selectedValuePath);
            }

            if (!string.IsNullOrEmpty(displayMemberPath))
            {
                comboBox.SetValue(ItemsControl.DisplayMemberPathProperty, displayMemberPath);
            }

            var itemsSourceBinding = new Binding { Source = itemsSource };
            comboBox.SetBinding(ItemsControl.ItemsSourceProperty, itemsSourceBinding);

            var valueBinding = new Binding { Path = new PropertyPath(valuePath) };
            comboBox.SetBinding(Selector.SelectedValueProperty, valueBinding);

            dataTemplate.VisualTree = comboBox;
            dataTemplate.Seal();
            return dataTemplate;
        }
    }
Then I use that generated DataTemplate as part of an EditorDefinition.

Keep in mind that the FrameWorkElementFactory has been deprecated in favor of XamlReader.Load(), but I didn't know how to connect the ItemsSource property that I passed in when creating the DataTemplate from XAML.
Oct 23, 2013 at 2:24 AM
I have the same problem, but propertygrid isn't work. I use V2.0

michmela44 wrote:
Just in case someone else has this same problem, I'll submit here what my current workaround is until the PropertyGrid supports a more flexible way to do it.
    public static class PropertyGridHelper
    {
        public static DataTemplate GetComboBoxEditorTemplate(object itemsSource, string selectedValuePath, string displayMemberPath, string valuePath)
        {
            var dataTemplate = new DataTemplate();
            var comboBox = new FrameworkElementFactory(typeof(ComboBox));
            comboBox.SetValue(FrameworkElement.MarginProperty, new Thickness(1, 1, 1, 1));
            if (!string.IsNullOrEmpty(selectedValuePath))
            {
                comboBox.SetValue(Selector.SelectedValuePathProperty, selectedValuePath);
            }

            if (!string.IsNullOrEmpty(displayMemberPath))
            {
                comboBox.SetValue(ItemsControl.DisplayMemberPathProperty, displayMemberPath);
            }

            var itemsSourceBinding = new Binding { Source = itemsSource };
            comboBox.SetBinding(ItemsControl.ItemsSourceProperty, itemsSourceBinding);

            var valueBinding = new Binding { Path = new PropertyPath(valuePath) };
            comboBox.SetBinding(Selector.SelectedValueProperty, valueBinding);

            dataTemplate.VisualTree = comboBox;
            dataTemplate.Seal();
            return dataTemplate;
        }
    }
Then I use that generated DataTemplate as part of an EditorDefinition.

Keep in mind that the FrameWorkElementFactory has been deprecated in favor of XamlReader.Load(), but I didn't know how to connect the ItemsSource property that I passed in when creating the DataTemplate from XAML.
Developer
Oct 23, 2013 at 12:17 PM
Hi,
Since v2.0, the "Plus" version includes a PropertyGrid.PropertiesSource property as long as PropertyGrid.PropertyNameBinding and PropertyGrid.PropertyValueBinding in order to modify at runtime the propertyGrid source. Please have a llok at the Click Once App available on https://wpftoolkit.codeplex.com/