Empty Image Mouse Click

非 Y 不嫁゛ 提交于 2019-12-24 05:44:10

问题


I am placing an Image in a Grid layout in C# with WPF. I want to have a mouse click event for it, but if the Image source is not set then the event is never fired. Is there a way to change that? I want the event to fire regardless of if the Image source is set or not.

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

The mouse events are never fired unless the property Source is set.


回答1:


Place you Image inside a Grid and register the events on the Grid

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



回答2:


 <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>



回答3:


<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>


来源:https://stackoverflow.com/questions/15525120/empty-image-mouse-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!