我正在創建包含多個零件的自定義控件。裏面創建模板,我訂閱了不同的事件,像這樣:何時取消訂閱自定義控件中的事件
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.partAreaCode = this.GetTemplateChild(PartAreaCode) as TextBox;
this.partExchange = this.GetTemplateChild(PartExchange) as TextBox;
this.partSubscriber = this.GetTemplateChild(PartSubscriber) as TextBox;
if (this.partAreaCode == null || this.partExchange == null || this.partSubscriber == null)
{
throw new NullReferenceException("Template part(s) not available");
}
this.partAreaCode.KeyDown += this.AreaKeyDown;
this.partAreaCode.TextChanged += this.AreaTextChanged;
this.partExchange.KeyDown += this.ExchangeKeyDown;
this.partExchange.TextChanged += this.ExchangeTextChanged;
this.partSubscriber.KeyDown += this.SubscriberKeyDown;
// Template might be applied after dependency property set
// lets refresh UI in this case
this.UIFromValue();
}
所以,我不知道是否應該從這些事件中,如果是取消 - 在哪裏以及如何?
太好了。我希望我可以發佈我的代碼,讓有知識的人閱讀它:)這是我的第一個控制 – katit
+1沒有想到這一點。如果您要更改模板,則不想在存儲器中保留未使用的PART元素。 – dowhilefor