另一个按钮的模板内的按钮 [英] Button inside template of another button

查看:43
本文介绍了另一个按钮的模板内的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个看起来像图块的按钮模板:

I've created a button template that looks like a tile:

当用户单击图块时,它会像这样展开:

When the user clicks the tile, it expands like so:

问题是,当用户单击磁贴中的按钮时,将触发磁贴按钮的单击处理程序以及模板内的按钮处理程序.

The problem is, when the user clicks a button inside the tile, the click handler for the tile button is triggered as well as the handler for the button inside the template.

一个超级简单的例子是:

A super simple example is:

XAML:

<Button Click="Outer_Click">
  <StackPanel>
    <TextBlock Text="Outer"
               HorizontalAlignment="Center" />
    <Button Click="Inner_Click"
            Content="Inner" />
  </StackPanel>
</Button>

C#:

private void Outer_Click(object sender, RoutedEventArgs e) {
  MessageBox.Show("Outer!");
}

private void Inner_Click(object sender, RoutedEventArgs e) {
  MessageBox.Show("Inner!");
}

如果用户单击内部"按钮,是否有任何方法不处理外部"点击事件?

Is there any way to not handle the "outer" click event if the user has clicked the "inner" button?

推荐答案

使用HighCore和开发刺猬留下的评论,这解决了我的问题:

Using comments left by HighCore and dev hedgehog, this solved my problem:

private void Inner_Click(object sender, RoutedEventArgs e) {
  MessageBox.Show("Inner!");
  e.Handled = true;
}

这篇关于另一个按钮的模板内的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆