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