Setting Forecolor of PropertyGrid

Grids for WPF Forum

Posted 10 years ago by Balaram Barange
Version: 4.5.0486
Avatar
Hi,

We have a project requirement that when the user changes the property in the property grid control should display the changed value in RED(change forecolor).

Presently we have only one option to set "ModifiedPropertyDisplayMode" which allows BoldValue and ItalicValue options.

Please suggest a way to achive this.

We made sample application to set ForeColor to Red once property has been changed, but it requires to call PropertyControl.Refresh(). Please have a look at following example and suggest the way to avoid call "Refresh()" which flickers the entire property items.

Example:

class MyBackGround // Sample Class for custom binding
    {
        private SolidColorBrush backgroundBrush = null;

        public MyBackGround()
        {
            backgroundBrush = new SolidColorBrush(Colors.Black);
        }

        public SolidColorBrush BackgroundColor
        {
            set { backgroundBrush = value; }
            get { return backgroundBrush; }
        }
    }

SolidColorBrush redBrush = new SolidColorBrush(Colors.Red);
SolidColorBrush blackBrush = new SolidColorBrush(Colors.Black);
MyBackGround myback = new MyBackGround(); // For Sample it is a class member.
private void Init() // My Init Function
        {
            Binding backGroundBinding = new Binding("BackgroundColor");
            backGroundBinding.Source = myback;

            DataTemplate dataTemplate = new DataTemplate();
            FrameworkElementFactory testFec = new FrameworkElementFactory(typeof(TextBox));
            testFec.SetBinding(TextBox.TextProperty, new Binding("Value") { RelativeSource = new RelativeSource() { AncestorType = typeof(IPropertyDataAccessor) } });
            testFec.SetBinding(TextBox.ForegroundProperty, backGroundBinding);
            dataTemplate.VisualTree = testFec;

            PropertyGridPropertyItem item = new PropertyGridPropertyItem();
            item.DisplayName = "Test Property";
            item.Description = "Description of test";
            item.Category = "General Properties";
            item.ValueTemplate = dataTemplate;
            item.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(item_PropertyChanged);
            item.Value = "Maulik";
            item.ValueName = "testBackG";
            this.propGrid.Properties.Add(item); // Add it.
        }

        void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName != "Value")
            {
                return;
            }
            if (propGrid.Properties.Count <= 0)
                return;

            PropertyGridPropertyItem propItem = (PropertyGridPropertyItem)sender;
            if (propItem.ValueName == "testBackG")
            {
                if ((bool)propItem.IsModified)
                {
                    myback.BackgroundColor = redBrush;
                    propGrid.Refresh(); // IF We comment it, the effect is not coming.
                }
            }
        }
Thanks,
Balaram

[Modified at 05/01/2009 04:48 AM]

Comments (2)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Balaram,

You need to implement the INotifyPropertyChanged interface on your MyBackGround class. Then you would fire the PropertyChanged event when the BackgroundColor property is changed (such as in the set accessor).


Actipro Software Support

Posted 10 years ago by Balaram Barange
Avatar
Thank you very much.
The latest build of this product (v2019.1 build 0681) was released 9 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.