c# - Click Event for WPF Image -


i porting old winforms desktop application wpf. app gui used winform's picturebox display images. old winforms app had onclick event handlers pictureboxes. clicking images did important. re-doing ui in wpf, found out per this equivalent winform's picturebox control wpf's image. however, when opened properties panel wpf image, there no click event handled, couldn't write click event handler had in winforms.

so, can please tell me can done achieve equivalent of winform's picturebox , it's click event in wpf? want display images , handle case each time user clicks image.

in wpf each control has default template (how looks) can change these templates , make controls want. makes easier pick control functionality , make want. in case want click choose button , change template

<window ...>     <window.resources>         <style targettype="{x:type button}" x:key="imagebuttonstyle">             <setter property="template">                 <setter.value>                     <controltemplate targettype="{x:type button}">                         <contentpresenter/>                     </controltemplate>                 </setter.value>             </setter>         </style>     </window.resources>     <button style="{staticresource imagebuttonstyle}" click="imagebutton_click">         <image source="..."/>     </button> </window> 

with above xaml image button

edit

below can find simplified version of how bind/change image.source done in mainwindow in wpf don't manipulate controls bind properties using binding , manipulate these properties. create dedicated class (viewmodel). class need implement inofitypropertychanged interface, datacontext needs set accordingly , bound property needs raise inofitypropertychanged.propertychanged event each time value changed (that's how notify ui refresh value)

public partial class mainwindow : window, inotifypropertychanged {    public mainwindow()    {       initializecomponent();       datacontext = this;    }     private imagesource _myimagesource;     public imagesource myimagesource    {       { return _myimagesource; }       set       {           _myimagesource = value;           onpropertychanged("myimagesource");       }    }     private void imagebutton_click(object sender, routedeventargs e)    {        this.myimagesource = new bitmapimage(...); //you change source of image    }     public event propertychangedeventhandler propertychanged;     private void onpropertychanged(string propertyname)    {       var handler = propertychanged;       if (handler != null) handler(this, new propertychangedeventargs(propertyname));    }     } 

and in xaml:

<button style="{staticresource imagebuttonstyle}" click="imagebutton_click" width="..." height="...">     <image source="{binding myimagesource}"/> </button> 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -