Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Property in PropertyGrid is not updated until focused manually

$
0
0

Hello,

I am trying to update a property value from a Form dialogue. This form dialogue is opened in the DoWork method ofBackgroundWorker

I have a custom type editor to update the property in the question.

Here is my code:

class UIFocusTracingEditor : UITypeEditor
    {
        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            if (context != null)
            {
                return System.Drawing.Design.UITypeEditorEditStyle.Modal;
            }
            return base.GetEditStyle(context);
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (value is string || value == null)
            {                               
                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += new DoWorkEventHandler(worker_DoWork);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

                worker.RunWorkerAsync(context);                   
            }
            return base.EditValue(context, provider, value);
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            ITypeDescriptorContext context = (ITypeDescriptorContext)e.Argument;

            using (var mw = new MainWindow())
            {

                var result = mw.ShowDialog();

                if (result == DialogResult.OK)
                {
                    string[] items = mw.targetItemPath.Split(';');

                    string[] arr = mw.targetItemPath.Split(';');
                    Array.Reverse(arr);
                    mw.targetItemPath = string.Join(";", arr);//Update the proeprty
                    ((ITargetElementPath)context.Instance).TargetElementPath = mw.targetItemPath;
                }
            }
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //CommandManager.InvalidateRequerySuggested(); //not working
        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
        }
    }

    public interface ITargetElementPath
    {
        string TargetElementPath { get; set; }
    }

I hope you understand the code. In above code, TargetElementPath is the property which I want to update.

As suggested by someone on stackoverflow, I tried  CommandManager.InvalidateRequerySuggested() to focus on the property in PropertyGrid. But it didn't work.

Could you please tell me the right solution?

Thank you in advance!



Viewing all articles
Browse latest Browse all 2535

Trending Articles