在 WPF 中,我可以像在 Javascript/Jquery 中一样将相同的单击处理程序同时附加到多个按钮吗?

发布于 2024-10-13 18:41:32 字数 406 浏览 0 评论 0原文

我有多个按钮而不是做

this.button1.Click += new System.EventHandler(this.button_Click);
this.button2.Click += new System.EventHandler(this.button_Click);
etc.
this.button10.Click += new System.EventHandler(this.button_Click);

我希望能够在伪代码中执行类似的操作:

this.button*.Click += new System.EventHandler(this.button_Click);

在 Javascript 中可能有类似的东西在 WPF 中吗?

I have multiple buttons instead of doing

this.button1.Click += new System.EventHandler(this.button_Click);
this.button2.Click += new System.EventHandler(this.button_Click);
etc.
this.button10.Click += new System.EventHandler(this.button_Click);

I'd like to be able to do something like this in pseudo-code:

this.button*.Click += new System.EventHandler(this.button_Click);

In Javascript it is possible is there something like that in WPF ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀子花开つ 2024-10-20 18:41:32

在 WPF 中,Button.Click 是一个路由事件,这意味着事件将在可视化树中向上路由,直到被处理。这意味着您可以在 XAML 中添加一个事件处理程序,如下所示:

<StackPanel Button.Click="button_Click">
    <Button>Button 1</Button>
    <Button>Button 2</Button>
    <Button>Button 3</Button>
    <Button>Button 4</Button>
</StackPanel>

现在所有按钮都将为其 Click 事件共享一个处理程序 (button_Click)。

这是在同一父容器中的一组控件之间处理同一事件的简单方法。如果您想从代码中执行相同的操作,可以使用 AddHandler 方法,如下所示:

AddHandler(Button.ClickEvent, new RoutedEventHandler( button_Click));

这将为窗口中的每个按钮单击添加一个处理程序。您可能想要为您的 StackPanel 指定一个名称(例如“stackPanel1”)并仅为该容器执行此操作:

stackPanel1.AddHandler(Button.ClickEvent, new RoutedEventHandler( button_Click));

In WPF, Button.Click is a routed event, which means that the event is routed up the visual tree until it's handled. That means you can add an event handler in your XAML, like this:

<StackPanel Button.Click="button_Click">
    <Button>Button 1</Button>
    <Button>Button 2</Button>
    <Button>Button 3</Button>
    <Button>Button 4</Button>
</StackPanel>

Now all the buttons will share a single handler (button_Click) for their Click event.

That's an easy way to handle the same event across a group of controls that live in the same parent container. If you want to do the same thing from code, you can use the AddHandler method, like this:

AddHandler(Button.ClickEvent, new RoutedEventHandler( button_Click));

That'll add a handler for every button click in the window. You might want to give your StackPanel a name (like, "stackPanel1") and do it just for that container:

stackPanel1.AddHandler(Button.ClickEvent, new RoutedEventHandler( button_Click));
许仙没带伞 2024-10-20 18:41:32

不要制作 button1button2 等,而是制作一个按钮列表。

然后您可以编写:

myList.ForEach( b => b.Click += button_Click );

从 XAML 您可以将单击处理程序附加到按钮的父级,如下所示(我使用 StackPanel 作为示例):

<StackPanel Button.Click="button_Click">
  <Button .... >First button</Button>
  <Button .... >Second button</Button>
  <Button .... >Third button</Button>
</StackPanel>

这有效,因为按钮 Click 事件是一个冒泡路由事件

Instead of making button1, button2 etc, make a List of buttons.

Then you can write:

myList.ForEach( b => b.Click += button_Click );

From XAML you can attach the click handler to the parent of the buttons something like this (I use a StackPanel as an example):

<StackPanel Button.Click="button_Click">
  <Button .... >First button</Button>
  <Button .... >Second button</Button>
  <Button .... >Third button</Button>
</StackPanel>

This works because the buttons Click event is a bubbling routed event.

衣神在巴黎 2024-10-20 18:41:32

您可以使用 Linq To VisualTree找到 Window / UserControl 中的所有按钮,然后迭代此列表以添加事件处理程序。

var buttons = this.Descendants<Button>().Cast<Button>();
foreach(var button in buttons)
{
  button.Click += button_Click;
}

我认为这已经是您所能得到的最简洁的了!

You could use Linq To VisualTree to locate all the buttons in your Window / UserControl, then iterate over this list adding your event handler.

var buttons = this.Descendants<Button>().Cast<Button>();
foreach(var button in buttons)
{
  button.Click += button_Click;
}

I think that is about as concise as you are going to get it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文