Can't "force" values with ComboBoxPropertyEditor

Grids for WPF Forum

Posted 9 years ago by SledgeHammer01
Version: 10.1.0523
Platform: .NET 4.0
Environment: Windows XP (32-bit)
Avatar
I have a window with a property grid. All stock, no theming, no special code, no event handlers, etc.

I have the following class:

    class Test : INotifyPropertyChanged
    {
        private int _a = 5;
        private bool _b = false;
        private bool _c = false;

        public int A
        {
            get
            {
                return _a;
            }

            set
            {
                if (value != _a)
                {
                    _a = 5;
                    FirePropertyChangeNotification("A");
                }
            }
        }

        public bool B
        {
            get
            {
                return _b;
            }

            set
            {
                if (value != _b)
                {
                    _b = false;
                    FirePropertyChangeNotification("B");
                }
            }
        }

        [Editor(typeof(ComboBoxPropertyEditor), typeof(PropertyEditor))]
        public bool C
        {
            get
            {
                return _c;
            }

            set
            {
                if (value != _c)
                {
                    _c = false;
                    FirePropertyChangeNotification("C");
                }
            }
        }

        private void FirePropertyChangeNotification(String PropertyName)
        {
            if ((object)PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

I set propGrid.SelectedObject = new Test();

You'll notice that all three properties "force" certain values in the setter.

A: this is an edit box, and no matter what I put in that edit box, once I "accept it", it'll return to 5. Works correctly.

B: this is a bool with the checkbox editor. No matter how I try to click on the checkbox, it always stays turned off. Works correctly.

C: this a bool with the combobox editor. If I select True in the drop down and close, my setter gets called which sets the value to false, and then the getter is called which reads out a false as expected, BUT, the combobox still displays "True". <-- BUG.

[Modified at 09/07/2010 07:05 PM]

Comments (3)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hello,

This is a known issue with using two-way bindings with certain WPF controls. Basically, if a binding is pushing a value back to the underlying property and that property's setter changes the value, the binding doesn't always refresh the target.

This article explains one possible workaround, but it would require you to create a custom property editor. You could take our DataTemplate for the ComboBoxPropertyEditor, tweak the binding, then add it to your application resources. It should be picked up by the PropertyGrid then.


Actipro Software Support

Posted 9 years ago by SledgeHammer01
Avatar
Hi, thanks for the link. I tried the following with no luck:

1) copy ComboBoxStringValueTemplateKey template and add IsAsync=True to the combobox binding and the text box binding.

2) modify the template to use local:ComboBoxEx (a class I created derived from ComboBox).

3) trap the DropDownClosed event in local:ComboBoxEx and call:

this.GetBindingExpression(ComboBox.SelectedItemProperty).UpdateTarget();

This handler is getting called, so I know everything is wired up properly, but the value in the combobox does not change even though the getter is returning the correct value.

Any ideas?
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
You would need to copy the ComboBoxValueTemplateKey, as that is the DataTemplate used by ComboBoxPropertyEditor (which your code uses). ComboBoxStringValueTemplateKey references the DataTemplate used by ComboBoxStringPropertyEditor.

Did you try a test outside of the PropertyGrid (i.e. just a ComboBox bound to a custom property)? If so and that worked fine, but it doesn't work in the PropertyGrid, then please put together a small sample project and email it to our support address. Be sure to remove any executables from the zip archive or rename the extension.


Actipro Software Support

The latest build of this product (v2019.1 build 0682) was released 4 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.