c# - ListBox SelectionChanged only once? -


i have listbox add items to. each time select item object of person person properties should showed in textboxes. person have person properties age, name, sex , on.

my listbox selection changed event triggers 1 time or on new added items. doesn't trigger when click , not added.

mainwindow.xaml.cs

namespace gui_wpf_eksamen { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window {     backlog bl = new backlog();      public mainwindow()     {          initializecomponent();          datacontext = bl;         this.prioritycombobox.items.add("high");         this.prioritycombobox.items.add("medium");         this.prioritycombobox.items.add("low");     }       private void addtoproductbacklogbtn_click(object sender, routedeventargs e)     {         this.productbackloglist.items.add(bl);         this.nametextbox.text = string.empty;     }       private void productbackloglist_selectionchanged(object sender, selectionchangedeventargs e)     {         var item = this.productbackloglist.selecteditem backlog;         this.namelabel.content = item.name;         this.descriptiontextblock.text = item.description;         this.prioritylabel.content = item.priority;         this.timelabel.content = item.time;     }      private void addtosprint_click(object sender, routedeventargs e)     {         var currentitem = this.productbackloglist.selecteditem backlog;         this.sprintbackloglist.items.add(currentitem);      } } } 

mainwindow.xaml

<window x:class="gui_wpf_eksamen.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:local="clr-namespace:gui_wpf_eksamen"     title="gui wpf application" height="800" width="1200"> <window.background>     <imagebrush imagesource="images/chalkboard.jpg"/> </window.background> <grid>     <grid.rowdefinitions>         <rowdefinition height="69*"/>         <rowdefinition height="700*"/>     </grid.rowdefinitions>     <label x:name="title" content="scrum-board xxl" horizontalalignment="center" horizontalcontentalignment="center" margin="15,10,5,0" verticalalignment="top" width="1172" height="56" fontsize="36" fontstyle="italic" fontweight="bold" foreground="white"/>      <grid horizontalalignment="left" height="680" margin="15,10,0,0" grid.row="1" verticalalignment="top" width="354">         <grid.rowdefinitions>             <rowdefinition height="94*"/>             <rowdefinition height="44*"/>             <rowdefinition height="131*"/>             <rowdefinition height="53*"/>             <rowdefinition height="40*"/>             <rowdefinition height="46*"/>             <rowdefinition height="272*"/>         </grid.rowdefinitions>         <label content="navn" horizontalalignment="left" margin="0,44,0,0" verticalalignment="top" width="77" fontsize="16" foreground="white"/>         <label content="beskrivelse&#xa;" horizontalalignment="left" margin="0,10,0,0" verticalalignment="top" foreground="white" fontsize="16" height="31" grid.row="1"/>         <label content="prioritet" horizontalalignment="left" margin="0,7,0,0" verticalalignment="top" width="67" fontsize="16" foreground="white" grid.row="3"/>         <label content="estimeret tidsforbrug" horizontalalignment="left" margin="0,10,0,0" verticalalignment="top" foreground="white" fontsize="16" grid.row="4" grid.rowspan="2"/>         <textbox x:name="nametextbox" horizontalalignment="left" height="23" margin="51,52,0,0" textwrapping="wrap" verticalalignment="top" width="303" opacity="0.5" text="{binding path=name}"/>         <combobox x:name="prioritycombobox" horizontalalignment="left" margin="73,12,0,0" verticalalignment="top" width="152" rendertransformorigin="0.5,0.5" opacity="0.5" grid.row="3" selecteditem="{binding path=priority}">             <combobox.rendertransform>                 <transformgroup>                     <scaletransform/>                     <skewtransform/>                     <rotatetransform angle="0.036"/>                     <translatetransform/>                 </transformgroup>             </combobox.rendertransform>         </combobox>         <textbox x:name="timetextbox" horizontalalignment="left" height="23" margin="0,6,0,0" textwrapping="wrap" verticalalignment="top" width="204" opacity="0.5" grid.row="5" text="{binding path=time}"/>         <label content="time(r)" horizontalalignment="left" margin="209,38,0,0" verticalalignment="top" foreground="white" fontsize="16" width="57" grid.row="4" grid.rowspan="2"/>         <textbox x:name="descriptiontextbox" horizontalalignment="left" height="111" margin="0,10,0,0" grid.row="2" textwrapping="wrap" verticalalignment="top" width="354" opacity="0.5" text="{binding path=description}"/>         <button x:name="addtoproductbacklogbtn" content="add backlog item" horizontalalignment="left" grid.row="6" verticalalignment="top" width="200" height="27" margin="66,0,0,0" background="white" opacity="0.7" click="addtoproductbacklogbtn_click"/>      </grid>      <grid horizontalalignment="left" height="680" margin="374,10,0,0" grid.row="1" verticalalignment="top" width="808" rendertransformorigin="0.459,0.431">         <grid.columndefinitions>             <columndefinition width="79*"/>             <columndefinition width="47*"/>             <columndefinition width="76*"/>         </grid.columndefinitions>         <grid.rowdefinitions>             <rowdefinition height="209*"/>             <rowdefinition height="131*"/>         </grid.rowdefinitions>         <label content="navn" horizontalalignment="left" margin="10,10,0,0" grid.row="1" verticalalignment="top" fontsize="16" foreground="white" height="31" width="47"/>         <label content="beskrivelse" horizontalalignment="left" margin="10,55,0,0" grid.row="1" verticalalignment="top" foreground="white" fontsize="16" height="31" width="87"/>         <label content="prioritet&#xd;&#xa;" horizontalalignment="left" margin="10,10,0,0" grid.row="1" verticalalignment="top" foreground="white" fontsize="16" height="32" grid.column="1" width="66"/>         <label content="estimeret tidsforbrug" grid.column="1" horizontalalignment="left" margin="10,55,0,0" grid.row="1" verticalalignment="top" foreground="white" fontsize="16" height="31" width="159"/>         <label x:name="prioritylabel" content="" grid.column="1" horizontalalignment="left" margin="90,10,0,0" grid.row="1" verticalalignment="top" fontsize="16" foreground="white" width="79" height="31"/>         <label x:name="timelabel" grid.column="2" horizontalalignment="left" margin="10,55,0,0" grid.row="1" verticalalignment="top" foreground="white" fontsize="16" width="208" height="31"/>         <listbox x:name="productbackloglist" horizontalalignment="left" height="353" margin="10,55,0,0" verticalalignment="top" width="292" displaymemberpath="name" opacity="0.505" selectionchanged="productbackloglist_selectionchanged" rendertransformorigin="0.5,0.5">             <listbox.rendertransform>                 <transformgroup>                     <scaletransform/>                     <skewtransform angley="-0.195"/>                     <rotatetransform/>                     <translatetransform y="-0.497"/>                 </transformgroup>             </listbox.rendertransform>         </listbox>         <listbox x:name="sprintbackloglist" horizontalalignment="left" height="353" margin="10,55,0,0" verticalalignment="top" width="284" displaymemberpath="name" grid.column="2" opacity="0.5"/>         <label x:name="namelabel" horizontalalignment="left" margin="62,14,0,0" grid.row="1" verticalalignment="top" width="244" height="27" content="{binding path=name}" foreground="white"/>         <button x:name="addtosprint" content="add &gt;&gt;" grid.column="1" horizontalalignment="left" margin="54,201,0,0" verticalalignment="top" width="75" height="20" opacity="0.7" click="addtosprint_click"/>         <textblock x:name="descriptiontextblock" horizontalalignment="left" margin="10,86,0,0" grid.row="1" textwrapping="wrap" verticalalignment="top" height="166" width="296" foreground="white"/>     </grid> </grid> 

that way of selection changed can accomplished easily:

have here:

<window x:class="listboxselection.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="mainwindow" height="350" width="525"> <dockpanel>     <listbox itemssource="{binding persons}" issynchronizedwithcurrentitem="true" dockpanel.dock="top">         <listbox.itemtemplate>             <datatemplate>                 <stackpanel orientation="horizontal">                     <textblock text="{binding name}" margin="5,0"/>                     <textblock text="{binding age, stringformat=is {0} years old}" margin="5,0"/>                 </stackpanel>             </datatemplate>         </listbox.itemtemplate>     </listbox>     <stackpanel orientation="horizontal" datacontext="{binding persons}" dockpanel.dock="top">         <textblock text="you have selected: " fontweight="semibold"/>         <textbox text="{binding name, updatesourcetrigger=propertychanged}"                   margin="5,0"                   fontweight="semibold"                   verticalalignment="top"                  horizontalalignment="center"/>         <textbox text="{binding age, updatesourcetrigger=propertychanged}"                  margin="5,0"                  fontweight="semibold"                   verticalalignment="top"                   horizontalalignment="left"/>     </stackpanel>      <button content="add item" dockpanel.dock="top" width="60" verticalalignment="top" margin="20" click="btnadditem_click"/> </dockpanel> 

note down: issynchronizedwithcurrentitem="true"

from msdn:

gets or sets value indicates whether selector should keep selecteditem synchronized current item in items property.

what's next step? display selected item:

<stackpanel datacontext="{binding persons}"/> 

stackpanel not itemscontrol, not try display items in persons instead selected one.

the icollectionview interface contains member called currentitem. issynchronizedwithcurrentitem is: whenever item clicked on itemscontrol, sets currentitem collection view. icollectionview has 2 events: currentitemchanging , currentitemchanged. when issynchronizedwithcurrentitem property set, itemscontrol update selecteditem based on icollectionview's currentitem is.

further more, using updatesourcetrigger=propertychanged able update selected item , displayed in listbox.

ok, that's about, here rest of code:

public class person : inotifypropertychanged {     private string _name;      public string name     {         { return _name; }         set         {             _name = value;             propertychanged(this, new propertychangedeventargs("name"));         }     }      private int _age;      public int age     {         { return _age; }         set         {             _age = value;             propertychanged(this, new propertychangedeventargs("age"));         }     }     public event propertychangedeventhandler propertychanged = delegate { }; } 

and codebehind:

public partial class mainwindow : window {      public observablecollection<person> persons { get; set; }      public person selectedperson     {         { return (person)getvalue(selectedpersonproperty); }         set { setvalue(selectedpersonproperty, value); }     }      // using dependencyproperty backing store selectedperson.  enables animation, styling, binding, etc...     public static readonly dependencyproperty selectedpersonproperty =         dependencyproperty.register("selectedperson", typeof(person), typeof(mainwindow), new propertymetadata(null));      public mainwindow()     {         initializecomponent();         persons = new observablecollection<person>();         persons.add(new person() { name = "name1", age = 20 });         persons.add(new person() { name = "name2", age = 25 });         persons.add(new person() { name = "name3", age = 30 });         this.datacontext = this;     }      private void btnadditem_click(object sender, routedeventargs e)     {         persons.add(new person() { name = "nameadded", age = 50 });     } } 

as see, selectionchanged handler not needed anymore.

you may asking, selectedperson in code behind:

add binding on listbox:

selecteditem="{binding selectedperson}" 

and in addtosprint_click use selectedperson person instead of directly accessing listbox.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -