Creating a clickable image in WPF

喜欢而已 提交于 2019-12-18 10:53:16

问题


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.


回答1:


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
        }



回答2:


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>



回答3:


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.




回答4:


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



回答5:


I don't know about you guys but PreviewMouseDown and TouchUp worked completely fine along with my binding:

<Image x:Name="bla" Source="{Binding blabla}" ... TouchDown="bla_TouchDown" PreviewMouseDown="bla_PreviewMouseDown">

I am using VS 2015



来源:https://stackoverflow.com/questions/2720463/creating-a-clickable-image-in-wpf

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