在成员创建和属性初始化之间的自定义控件中插入代码 [英] Insert code in a Custom Control between member create and property initialization

查看:80
本文介绍了在成员创建和属性初始化之间的自定义控件中插入代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试开发一个继承自ComboBox的WPF自定义控件.

自定义控件包含引用其他类的属性.在运行时,属性引用的类是在用户控件的构造函数执行之后并且在初始化类的内部属性之后自动创建的.

我想在属性的创建和初始化(类的内部属性)之间执行代码,但是我找不到位置或方式,因此尝试覆盖BeginInit,Initialized,Loaded没有结果,似乎创建和初始化产生是一个块.

有什么办法吗?

谢谢大家的帮助.

我将尝试说明这一点.我把数字< x>为了显示顺序,我想执行3到4之间的代码:

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>
	}
}

推荐答案

只需创建5个事件;在您的占位符所在的地方发起每个事件.例如.

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

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());
}



考虑通过子类使用自定义事件参数. 参见例如:
在C#中使用事件和代理 [ C#中的事件和事件处理 [



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


这篇关于在成员创建和属性初始化之间的自定义控件中插入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆