开发者

How do i reuse event handler?

I got a code:

private void border1_PreviewGiveFeedback(object sender, GiveFeedbackEventArgs e)
{
    // so开发者_高级运维me code
}

mainwindow.xaml:

<Border BorderBrush="Silver" PreviewGiveFeedback="border1_PreviewGiveFeedback"
        Name="border1" />

Now i want to reuse the event handler in border1 inside border2 (without writig the same code in border2 and cs file) What to write here:

<Border Name="border2" />


I think the same as you use in the first border

<Border PreviewGiveFeedback="border1_PreviewGiveFeedback" Name="border2" />


The PreviewGiveFeedback property is simply asking for an event handler with a certain signature. You could actually name it Potatoes_AreTasty__YUM and it would function perfectly well as long as it had the proper signature.

Thus, the way to use it in border2 is to just give it the same event method as border1. You could also re-name the function to indicate that it isn't exclusively for border1 but a generic border preview event handler.


You can do exactly the same:

<Border Name="border2" PreviewGiveFeedback="border1_PreviewGiveFeedback" />

is going to work. You should consider renaming the event handler method in that case, though.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜