wpf - Same click event for both Button and Grid -


i read routed events today , tried provide same click event handler both normal button , custom grid button. stackpanel handling routed button click event, , invoke same handler firing click event grid's mousedown event. code without error not working expected. button click brings messagebox clicking grid mouse nothing.

<window x:class="routedeventpr.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="mainwindow" height="550" width="525">  <stackpanel  background="transparent" button.click="button_click_1">      <grid width="200" height="100" background="aqua" mousedown="grid_mousedown_1">         <ellipse strokethickness="4" width="200" height="100" fill="beige"/>                    <textblock text="press" horizontalalignment="center" verticalalignment="center"/>     </grid>      <button x:name="surebtn" content="pause !" width="200" margin="0 10 0 0"/>      <image horizontalalignment="center" verticalalignment="center" width="200" height="200" source="i://fancybtn.jpg"/>      <label content="start game"/>  </stackpanel> 

    private void grid_mousedown_1(object sender, mousebuttoneventargs e)     {         raiseevent(new routedeventargs(button.clickevent, this));     }      private void button_click_1(object sender, routedeventargs e)     {         messagebox.show("are sure !");     } 

button.click event routed event bubbling strategy set bubble.

that means bubble visual parent till root until handled. in case, you raise event window, bubble window parent null.

you have raise event child control of stackpanel can bubble stackpanel.

xaml

<grid x:name="grid" width="200" height="100" background="aqua"        mousedown="grid_mousedown_1">     <ellipse strokethickness="4" width="200" height="100" fill="beige"/>     <textblock text="press" horizontalalignment="center"                verticalalignment="center"/> </grid> 

code behind

private void grid_mousedown_1(object sender, mousebuttoneventargs e) {     grid.raiseevent(new routedeventargs(button.clickevent, this)); } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -