空图像鼠标单击

时间:2013-03-20 13:36:12

标签: c# wpf image mouseclick-event

我正在使用WPF在C#中将图像放置在网格布局中。我希望有一个鼠标单击事件,但如果未设置图像源,则永远不会触发该事件。有办法改变吗?无论是否设置了图像源,我都希望触发事件。

<Image Focusable="True" IsEnabled="True" Grid.Row="1" Grid.Column="0"
MouseLeftButtonDown="Image_MouseLeftButtonDown" MouseDown="Image_MouseDown" Stretch="Fill" />

除非设置了属性Source,否则永远不会触发鼠标事件。

3 个答案:

答案 0 :(得分:1)

Image放入Grid并在网格上注册事件

<Grid Grid.Row="1" Background="White" Grid.Column="0" MouseLeftButtonDown="Image_MouseLeftButtonDown" MouseDown="Image_MouseDown">
  <Image Focusable="True" IsEnabled="True" Stretch="Fill" />
</Grid>

答案 1 :(得分:0)

 <Grid>
 <Image Height="150" HorizontalAlignment="Left" Margin="218,18,0,0" Name="image1"   Stretch="Fill" VerticalAlignment="Top" Width="200" Source="path" MouseLeftButtonDown="image1_MouseLeftButtonDown" />
 </Grid>

答案 2 :(得分:0)

<Grid>
    <Image Height="150" HorizontalAlignment="Left" Margin="218,18,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="/WpfApplication1;component/Images/2012-12-14-1535%20%281%29.jpg" MouseLeftButtonDown="image1_MouseLeftButtonDown" />
    <Rectangle Height="150" HorizontalAlignment="Left" Margin="218,18,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="200" />
 </Grid>