开发者

UserControl inheritance problem in Silverlight

If i define any event handler in base UserControl class and try to create the instance of derived class i get exceptions similar to that:

Failed to assign to property 'System.Windows.Controls.Primitives.ButtonBase.Click'. [Line: 32 开发者_运维问答Position: 54]


If you wish to derive a UserControl with xaml, you must subscribe all events in code behind, otherwise when parsing the xaml, silverlight will try to find the event handler on the subclass instance and fail, hence the error you're describing.


You can attach the handlers in the code behind.


You can make all of the event handlers in your UserControl protected, then override them in the derived class:

new protected void MyButton_Click(object sender, RoutedEventArgs e)
{
    base.MyButton_Click(sender, e);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜