This project is read-only.

Event Handling Of the propertygrid control

Mar 25, 2011 at 11:36 AM

Hi all,

        I want to see the basic even handling mechanism of the property grid controls. Can anyone please help me by providing some solid examples please? i have tried to search here, but couldn't find any thing suitable.

 

       Example Of Handling combobox control events will be a great help.

 

Regards

Mar 28, 2011 at 8:22 AM

Hello.

I can suggest you a solution...

In PropertyGrid class create an event fpr example "public event WPGUIEventHandlers.UIPropertyContentEventFiredEventHandler UIPropertyContentEventFired":

 

public static class WPGUIEventHandlers
    {
        public delegate void UIPropertyContentEventFiredEventHandler(object propertyInstance, UIEventArgs args);
    }

 

In UIEventArgs you can hold what ever you want. In my - property grid control instance and property grid control EventArgs instance:

 

public class UIEventArgs
    {
        public object UISender { get; private set; }
        public System.EventArgs UIArgs { get; private set; }

        internal UIEventArgs(object uiSender, System.EventArgs eventArgs)
        {
            this.UISender = uiSender;
            this.UIArgs = eventArgs;
        }
    }

 

To fire events i created WPGComboBox class that derivers for ComboBox class and overrides it's methods. In my case it's OnSelectionChanged(...):

 

public class WPGComboBox : ComboBox
    {
        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);
            WPGUIEventFactory.Fire(this.GetParentPropertyGrid(), this.DataContext as Property, this, e);
        }

        private PropertyGrid GetParentPropertyGrid()
        {
            var parent = VisualTreeHelper.GetParent(this);
            while (!(parent is PropertyGrid))
            {
                parent = VisualTreeHelper.GetParent(parent);
                if (parent == null)
                    break;
            }
            return parent as PropertyGrid;
        }
    }

 

To fire property grid event i just create event factory as follows:

public static class WPGUIEventFactory
    {
        public static void Fire(PropertyGrid propertyGrid, Property property, object uiSender, System.EventArgs args)
        {
            if (propertyGrid != null)
                propertyGrid.FireUIEvent(property, uiSender, args);
        }
    }

And don't forget to change WPGTemplates.xaml:

<DataTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:PropertyGrid}, ResourceId={x:Type clr:Enum}}">
		<local:WPGComboBox Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type local:PropertyGrid}, ResourceId=ComboBoxStyle}}" SelectedValue="{Binding Path=Value}">
			<ComboBox.ItemsSource>
				<Binding Path="Value">
					<Binding.Converter>
						<converters:EnumTypeConverter />
					</Binding.Converter>
				</Binding>
			</ComboBox.ItemsSource>
        </local:WPGComboBox>
</DataTemplate>

And in PropertyGrid class add a method that fires "UIPropertyContentEventFired" event:

 public class PropertyGrid : Control
    {
              .....

internal void FireUIEvent(Property property, object uiSender, System.EventArgs args)
        {
            if (this.UIPropertyContentEventFired != null)
                this.UIPropertyContentEventFired(property.Instance, new UIEventArgs(uiSender, args));
        }
             ......
}

That's all. If you found better way to do that or you have any questions please let me know.

Regards