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.
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>
<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>
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.