在 WPF 中创建可点击的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2720463/
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
Creating a clickable image in WPF
提问by mico
I want to make a user control that shows an image and can invoke a command when clicked. Later I want to bind a list of these controls to a list of products.
我想制作一个显示图像并可以在单击时调用命令的用户控件。后来我想将这些控件的列表绑定到产品列表。
回答by GibboK
Try this very straight forward approach
试试这个非常直接的方法
<Grid>
<Button Height="50" Width="50">
<Button.Template>
<ControlTemplate>
<Image Source="yourimage.png"/>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
// do smt
}
回答by mico
Well, after a little more fiddling, a simple button does the job. Here it is:
好吧,稍微摆弄一下,一个简单的按钮就可以完成这项工作。这里是:
<Button Command="{Binding Path=DisplayProductCommand}" >
<Image Source="..\Images\my-beautiful-product.jpg"/>
</Button>
回答by code-zoop
There are several ways to do this, but one simple solution would be to use a button (maybe style away the border and background), and use the image as the content of the button.
有几种方法可以做到这一点,但一个简单的解决方案是使用按钮(也许样式去掉边框和背景),并使用图像作为按钮的内容。
You can later use a ListBox or similar, and override the DataTemplate to use the button and an image for each product.
您可以稍后使用 ListBox 或类似的,并覆盖 DataTemplate 以使用每个产品的按钮和图像。
回答by Hong
<Image Name="imageFoo" Source="/AppFoo;component/Foo.png" Width="32" Cursor="Hand" MouseUp="imageFoo_MouseUp"/>
private void imageFoo_MouseUp(object sender, MouseButtonEventArgs e)
{
//Do something
}
回答by Khalil Khalaf
I don't know about you guys but PreviewMouseDown
and TouchUp
worked completely fine along with my binding:
我不了解你们,但是PreviewMouseDown
和TouchUp
我的绑定一起工作得很好:
<Image x:Name="bla" Source="{Binding blabla}" ... TouchDown="bla_TouchDown" PreviewMouseDown="bla_PreviewMouseDown">
I am using VS 2015
我正在使用 VS 2015