我正在创建包含多个零件的自定义控件。里面创建模板,我订阅了不同的事件,像这样:何时取消订阅自定义控件中的事件
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