How Can I Check/UnCheck All Checkboxes in Listbox in C# WPF? -


i have listbox contains checkboxes in datatemplate , have 2 buttons "select all" , "unselect all".i want make checkboxes check , uncheck clicking select , unselect buttons , want implement inotifypropertychanged class. how can things?

thanks answers in advance..

xaml code

  <stackpanel>         <listbox name="lstuserdefination"   scrollviewer.verticalscrollbarvisibility="auto" selectionmode="multiple">             <listbox.itemtemplate>                 <datatemplate>                     <listboxitem>                     <checkbox name="chkuser" content="{binding authorityname}"/>                     </listboxitem>                 </datatemplate>             </listbox.itemtemplate>         </listbox>     </stackpanel> 

c# code

   public partial class userdefinationedit : window {     public observablecollection<authority> authoritylist { get; set; }      public userdefinationedit()     {         initializecomponent();         createcheckboxlist();         lstuserdefination.itemssource = authoritylist;     }       private void window_mousedown(object sender, mousebuttoneventargs e)     {         if (e.leftbutton == mousebuttonstate.pressed)         {             dragmove();         }     }      public void createcheckboxlist()     {         authoritylist = new observablecollection<authority>();          authoritylist.add(new authority() {authorityvalue = 0, authorityname = "0 - " });         authoritylist.add(new authority() { authorityvalue = 1, authorityname = "1 - " });         authoritylist.add(new authority() { authorityvalue = 2, authorityname = "2 - " });         authoritylist.add(new authority() { authorityvalue = 3, authorityname = "3 - " });         authoritylist.add(new authority() { authorityvalue = 4, authorityname = "4 - " });         authoritylist.add(new authority() { authorityvalue = 5, authorityname = "5 - " });         authoritylist.add(new authority() { authorityvalue = 6, authorityname = "6 - " });           this.datacontext = this;     }      private void btnunselectall_click(object sender, routedeventargs e)     {         lstuserdefination.unselectall();     }      private void btnselectall_click(object sender, routedeventargs e)     {         lstuserdefination.selectall();     }  } public class authority {     public string authorityname { get; set; }     public int authorityvalue { get; set; }     public bool ischecked { get; set; } } } 

add binding ischecked property in listboxitem template

<checkbox name="chkuser" content="{binding authorityname}" ischecked="{binding ischecked}"/> 

and change button handlers

private void btnunselectall_click(object sender, routedeventargs e) {     foreach (var in authoritylist)     {         a.ischecked = false;     } }  private void btnselectall_click(object sender, routedeventargs e) {     foreach (var in authoritylist)     {         a.ischecked = true;     } } 

note authority class should implement inotifypropertychanged make work.

public class authority : inotifypropertychanged {     private string authorityname;     private int authorityvalue;     private bool ischecked;      public string authorityname     {         { return authorityname; }         set         {             authorityname = value;             notifypropertychanged();         }     }      public int authorityvalue     {         { return authorityvalue; }         set         {             authorityvalue = value;             notifypropertychanged();         }     }      public bool ischecked     {         { return ischecked; }         set         {             ischecked = value;             notifypropertychanged();         }     }       public event propertychangedeventhandler propertychanged;      private void notifypropertychanged(string propertyname = "")     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     } } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -