Click here to Skip to main content
15,888,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello, I'm trying to develop a WPF custom control which inherits from ComboBox.

The custom controls contains properties which references other classes. At runtime, the classes referenced by properties are created auto after the execution of the contructor of the user control and just after the initialization of the internal properties of the class are executed.

I want to execute code between the creation of the properties and the initialization (of the internal properties of the class) but I don't find where or how, I tried to override BeginInit, Initialized, Loaded with no results, it seems that the creation and initialization is produces is a block.

Is there any way to do this?

Thanks every body for your help.

I'm going to try ilustrating this. I put numbers <x> to show the sequence, I would like to execute code between 3 and 4:

class MyUserControl: ComboBoxEdit
{
	//Constructor
	public MyUserControl()
	{
		<1>
	}
	
	protected override void BeginInit()
	{
		<2>
		base.BeginInit();
		<5>
	}
	
	public MyProperty myProperty
	{
		get {...}
		set {...}
	}
}

class MyProperty 
{
	public Property1 { get {...} set {<4>...} }
	public Property2 { get {...} set {<4>...} }
	
	public MyProperty()
	{
		<3>
	}
}
Posted
Comments
Wjousts 2-Aug-11 17:03pm    
Where's 4?
charly1970 2-Aug-11 17:59pm    
it's in the "set" of the properties in MyProperty, it executes just after contructor <3>
Sergey Alexandrovich Kryukov 3-Aug-11 4:36am    
Please don't post non-answer as solution. Use "Improve question" or "Add comment" to a solution or reply to existing comment.

I did not understand why my suggestion does not resolve your problem. If you want better solution, please make use-case of what you want which would clearly illustrate your requirements.
--SA
charly1970 3-Aug-11 11:15am    
I want to be able to execute code inside MyUserControl class between steps <3> and <4>. BeginInit method (MyUserControl class) is responsible of creation of MyProperty class (step <3>) and it makes the initialization of its properties too (step <4>), so where I can put code in MyUserControl Class which executes between these two steps?
I know it's difficult to understand I've tried to make my example as easy as I can. I'm very grateful with you for your answers. Some way to illustrate this is divide the BeginInit method in two parts (if it were possible...):

protected override void BeginInit()
{
<2>
base.BeginInit_ForCreation(); // this would execute the part 3 in MyProperty class
Here my code....
base.BeginInit_ForInitialization(); // this would execute the part 4 in MyProperty class
<5>
}

1 solution

Just create 5 events; raise each event where your placeholders are. For example.

C#
public event System.EventHandler StartingInitialization;
public event System.EventHandler StartedInitialization;

//..

protected override void BeginInit() {
   if (StartingInitialization != null) //instead of #2
      StartingInitialization(this, new System.EventArgs());
   base.BeginInit();
   if (StartedInitialization != null) //instead of #5
      StartedInitialization(this, new System.EventArgs());
}


Consider using custom event arguments by sub-classing System.EventArgs.
See, for example:
Using Events and Delegates in C#[^],
Events and event handling in C#[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900