c# - Using observablecollection with buttons -


i have observablecollection create buttons problem each of these buttons call same event handler. trying make each button has own unique id can distinguished 1 another. code used create button elements is

public class button {     public bool isempty { get; set; }      public int id {get; set;}      public button(int button_number)     {         isempty = true;         id = button_number;      } } 

i added observablecollection in following code

        buttoncollection = new observablecollection<cchipvm>();          ( int = 0 ; < inumchips ; ++i )         {             buttoncollection.add( new button(i) );          } 

the xml button following

 <datatemplate x:key="buttontemplate">         <button x:name="button" uid="{binding path=id}" click="button_click" borderbrush="black" borderthickness="1" margin="7" width="25" height="25" clickmode="press" opacity="0.9" focusable="false" ishittestvisible="true" allowdrop="true" istabstop="false">             <button.style>                                     <style>                     <style.triggers>                         <datatrigger binding="{binding path=isempty}" value="false">                             <setter property="button.background">                                 <setter.value>                                     <lineargradientbrush endpoint="0.5,1" startpoint="0.5,0">                                         <gradientstop color="#ff5ed426" offset="0"/>                                         <gradientstop color="white" offset="1"/>                                     </lineargradientbrush>                                 </setter.value>                             </setter>                         </datatrigger>                         <datatrigger binding="{binding path=isempty}" value="true">                             <setter property="button.background">                                 <setter.value>                                     <lineargradientbrush endpoint="0.5,1" startpoint="0.5,0">                                         <gradientstop color="#ff1766f0" offset="0"/>                                         <gradientstop color="white" offset="1"/>                                     </lineargradientbrush>                                 </setter.value>                             </setter>                         </datatrigger>                      </style.triggers>                 </style>             </button.style>         </button>     </datatemplate> 

when tries set uid method system crashes, recommendation how fix or other solution each button can have unique id

you can use button.tag property store id , in handler inspect value of tag

<button tag="{binding path=id}" click="button_click" borderbrush="black" borderthickness="1" margin="7" width="25" height="25" clickmode="press" opacity="0.9" focusable="false" ishittestvisible="true" allowdrop="true" istabstop="false"> 

update : button click handler

public void button_click(object sender,eventargs e) {        var mybutton = (button)sender;       int id = convert.toint32(mybutton.tag); } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -