在 wpf 中单击图像外部时隐藏图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20238613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide image when click outside of image in wpf
提问by Behzad Ashrafian
When my WPF application loads, an image is shown in center. how can i handle mouse click outside of image. when user clicks outside of image it hide. My code is in c#.
当我的 WPF 应用程序加载时,图像显示在中心。我如何处理图像外的鼠标点击。当用户单击图像外部时,它会隐藏。我的代码是在 c# 中。
回答by Sheridan
You can simply add a handler to the top level control, eg. Grid, Window, etc. In that handler, you can check whether the control that was clicked on was the Imageand if it wasn't, then you could hide it:
您可以简单地将处理程序添加到顶级控件,例如。Grid、Window等。在该处理程序中,您可以检查点击的控件是否为Image,如果不是,则可以隐藏它:
The XAML:
XAML:
<Grid PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown">
...
<Image Name="TheImage" Source="/WpfApplication2;component/Images/Add_16.png" />
...
</Grid>
The code behind:
背后的代码:
private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource != TheImage)
{
TheImage.Visibility = Visibility.Hidden;
}
}
While this fulfils your requirements, it should be noted that once hidden, the Imagewill no longer be 'clickable'.
虽然这满足您的要求,但应该注意的是,一旦隐藏,Image将不再是“可点击的”。
回答by manish
just put the code MyImage.Visibility = System.Windows.Visibility.Hidden;in whichever event you want to capture.
只需将代码MyImage.Visibility = System.Windows.Visibility.Hidden;放在您想要捕获的任何事件中。
for eg:
例如:
private void MyButton_Click_1(object sender, RoutedEventArgs e)
{
MyImage.Visibility = System.Windows.Visibility.Hidden;
}
private void MyTextbox_PreviewMouseDown_1(object sender, MouseButtonEventArgs e)
{
MyImage.Visibility = System.Windows.Visibility.Hidden;
}
private void MyWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
MyImage.Visibility = System.Windows.Visibility.Hidden;
}
where MyImageis the name of image, MyTextboxis the name of textbox, MyButtonis the name of Button and MyWindowis the name of Main Window
其中MyImage是图像名称,MyTextbox是文本框名称,MyButton是按钮MyWindow名称,是主窗口名称
回答by Neel Bhasin
you can simply write
你可以简单地写
YourImageName.Visibility = Visibility.Hidden;
YourImageName.Visibility = Visibility.Hidden;
Write this code to any of the control i.e. TextBoxor Button.
将此代码写入任何控件 ieTextBox或Button。
回答by Shamim
first click on the form to select it. then go to the event's section (near property section) and double-click the click event to make a function call when a form is clicked. in the code compare the sender object with the IsEqual(obj)method and see the sender is same image or it's not. if not hide it. :)
首先单击表单以选择它。然后转到事件部分(靠近属性部分)并双击单击事件以在单击表单时进行函数调用。在代码中将发件人对象与IsEqual(obj)方法进行比较,看看发件人是相同的图像还是不是。如果不隐藏它。:)
回答by Malcolm
You can handle the click event of Application current window , and inside that event you can check if the mouse position is within that image or outside of that image , on basis of this you can set your image visibility condition you prefer .
您可以处理应用程序当前窗口的点击事件,在该事件中您可以检查鼠标位置是在该图像内还是该图像外,在此基础上您可以设置您喜欢的图像可见性条件。
Code sample example :
代码示例:
//registering event
Application.Curent.mainWindow.MouseRightButtonDown += MainWindow_MouseRightButtonDown;
//event implementation
void MouseRightButtonDown(object sender , MouseButtonEventArgs e)
{
//here you can check the ui element for image control using sender
//below will let you know the position of Click
e.GetPosition(// pass the ui element here)
}
Note : The above code shown is at app level click handling . If you dont want at app level you can take the parent xaml within which the image is present and do the same
注意:上面显示的代码是在应用级别点击处理。如果您不想在应用程序级别,您可以使用图像所在的父 xaml 并执行相同的操作

