我在运行时生成了几个包含内部东西的Border元素。我设法产生它们但现在我想将事件与MouseEnter,Click等相关联,但我不知道如何。 我设法将每个Border元素与一个唯一的名称(Name属性)相关联,因此如果我需要,我可以使用它们。如何在C#?
中为每个生成的Border元素添加事件答案 0 :(得分:2)
像这样使用
border.MouseEnter += new MouseEventHandler(border_MouseEnter);
void border_MouseEnter(object sender, MouseEventArgs e)
{
throw new NotImplementedException();
}
答案 1 :(得分:2)
<Style x:Key="BorderNameStyle" TargetType="{x:Type BorderName}">
<EventSetter Event="MouseEnter" Handler="itm_MouseEnter"></EventSetter>
<EventSetter Event="MouseLeftButtonUp" Handler="itm_MouseLeftButtonUp"></EventSetter>
<EventSetter Event="PreviewMouseRightButtonUp" Handler="itm_MouseRightButtonUp"></EventSetter>
<EventSetter Event="MouseLeave" Handler="itm_MouseLeave"></EventSetter>
<EventSetter Event="MouseDoubleClick" Handler="ItemMouseDoubleClick"></EventSetter>
</Style>
通过为Border或代码定义样式,您需要调用如下的路由事件。
border.MouseEnter += new MouseEventHandler(border_MouseEnter);
void border_MouseEnter(object sender, MouseEventArgs e)
{
Button btn = new Button();
btn.Click += new RoutedEventHandler(btn_Click);
}
void btn_Click(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}