【发布时间】:2013-12-12 23:54:58
【问题描述】:
我整理了以下 XAML 代码,试图找出为什么 Click 事件仅在我单击 Button 内的 Image 或 TextBlock 内容时触发。 Button 中这些周围的区域不会触发点击事件:
<Window x:Class="TestBedApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel Orientation="Horizontal" Background="Bisque">
<Border BorderThickness="2" BorderBrush="Aqua">
<Button x:Name="btn" Click="btn_Click" Background="Olive">
<Button.Template>
<ControlTemplate>
<Border BorderThickness="2" BorderBrush="Red">
<StackPanel Orientation="Vertical">
<Image x:Name="img"
Source="Images/MyImage.png"
Width="100"
Height="72"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="10">
</Image>
<TextBlock Text="Click Me" Foreground="White"/>
</StackPanel>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</Border>
</StackPanel>
</Window>
后面的代码:
private void btn_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Ooosh");
}
我在网上看到了一些使用 ContentPresenters 的示例,但我正在对此进行解释。我对 XAML 非常陌生,刚刚开始涉足。我正在使用 VS2013 和 .NET 4.5 应用程序。
【问题讨论】: