c# - Changing ListView's selected item from code -


i'm constructing application using wpf , i'm having trouble figuring out how display new selection in listview that's selected via code.

i have listview bunch of items. want put button moves selected item next item in view. have able deselect item, move next item, , select selection appears user.

my xaml code follows:

    <border grid.row="1" cornerradius="10" borderbrush="black" borderthickness="10">       <listview x:name="lvlogpackets" background="#ff0c3a58" foreground="white" selectionchanged="lvlogpackets_selectionchanged" selecteditem="{binding mode=twoway, updatesourcetrigger=propertychanged, path=isselected}">         <listview.contextmenu>           <contextmenu name="lvcmenu" opened="menuopened_click">             <menuitem header="filter checked" ischeckable="true" checked="menuviewcheckbox_checked" unchecked="menuviewcheckbox_unchecked"/>             <menuitem header="filter selected" ischeckable="true" checked="menuviewselected_checked" unchecked="menuviewselected_unchecked"/>             <separator />             <menuitem header="Δt: n/a"/>             <separator />             <menuitem header="pop out data" click="menupopout"/>             <separator />             <menuitem header="copy payload csv" click="menucopypayloadcsv"/>           </contextmenu>         </listview.contextmenu>         <listview.itemcontainerstyle>           <style targettype="listviewitem">             <style.triggers>               <trigger property="isselected" value="true">                 <setter property="background" value="green"/>               </trigger>               <trigger property="ismouseover" value="true">                 <setter property="background" value="red"/>               </trigger>             </style.triggers>           </style>         </listview.itemcontainerstyle>         <listview.view>           <gridview x:name="lvgridview">             <gridviewcolumn width="30">               <gridviewcolumn.celltemplate>                 <datatemplate>                   <checkbox ischecked="{binding path=ischecked, mode=twoway, updatesourcetrigger=propertychanged}" />                                       </datatemplate>               </gridviewcolumn.celltemplate>             </gridviewcolumn>             <gridviewcolumn header="index" width="100" displaymemberbinding="{binding path=index}"/>             <gridviewcolumn header="systime" width="100" displaymemberbinding="{binding path=systime}"/>             <gridviewcolumn header="elapsedtime" width="150" displaymemberbinding="{binding path=elapsedtime}"/>             <gridviewcolumn header="source" width="100" displaymemberbinding="{binding path=source}"/>             <gridviewcolumn header="destination" width="100" displaymemberbinding="{binding path=destination}"/>             <gridviewcolumn header="cmdid" width="100" displaymemberbinding="{binding path=cmdid}"/>             <gridviewcolumn header="payloadsize" width="100" displaymemberbinding="{binding path=payloadsize}"/>             <gridviewcolumn header="payload" width="800" displaymemberbinding="{binding path=payload}"/>           </gridview>         </listview.view>       </listview>     </border> 

my application code follows:

public class logitem : inotifypropertychanged {   public string index { get; set; }   public string systime { get; set; }   public string elapsedtime { get; set; }   public string source { get; set; }   public string destination { get; set; }   public string cmdid { get; set; }   public string payloadsize { get; set; }   public string payload { get; set; }    public bool _isselected;   public bool isselected   {     { return _isselected; }     set { _isselected = value; notifypropertychanged("isselected"); }   }    private bool _ischecked;   public bool ischecked    {     { return _ischecked; }     set { _ischecked = value; notifypropertychanged("ischecked"); }   }    ...    public event propertychangedeventhandler propertychanged;   protected void notifypropertychanged(string strpropertyname)   {     if (propertychanged != null)       propertychanged(this, new propertychangedeventargs(strpropertyname));   } }  public partial class mainwindow : ribbonwindow {   private observablecollection<logitem> m_logitems = new observablecollection<logitem>();   private void ribbonwindow_loaded(object sender, routedeventargs e)   {     lvlogpackets.itemssource = m_logitems;   } } 

all other bindings appear work correctly, including ischecked binding. missing here? how link selecteditem/s data updates correctly?

edit: added mainwindow code requested wyatt earp.

they mean need bind object in on viewmodel so

public class sample_model {     public sample_model(string artist, string song, string = "")     {         this.artist = artist;         this.song = song;         this.extra = extra;     }      public string artist { get; set; }     public string song { get; set; }     public string { get; set; } }  public class sample_viewmodel  : inotifypropertychanged  {     public sample_viewmodel()     {         this.datasource = createdata();      }      // implement inotify     public event propertychangedeventhandler propertychanged;     private void notifypropertychanged(string propertyname)     {         propertychangedeventhandler handler = propertychanged;         if (null != handler)         {             handler(this, new propertychangedeventargs(propertyname));         }     }      // create static list our demo     private observablecollection<sample_model> createdata()     {         observablecollection<sample_model> my_list = new observablecollection<sample_model>();         my_list.add(new sample_model("faith + 1", "body of christ", "a track"));         my_list.add(new sample_model("faith + 1", "christ again", "a track"));         my_list.add(new sample_model("faith + 1", "a night lord", "a track"));         my_list.add(new sample_model("faith + 1", "touch me jesus", "a track"));         my_list.add(new sample_model("faith + 1", "i found jesus (with else)", "a track"));         my_list.add(new sample_model("faith + 1", "savior self", "a track"));         my_list.add(new sample_model("faith + 1", "christ day", "a track"));         my_list.add(new sample_model("faith + 1", "three times savior", "a track"));         my_list.add(new sample_model("faith + 1", "jesus touched me", "a track"));         my_list.add(new sample_model("faith + 1", "lord savior", "a track"));         my_list.add(new sample_model("faith + 1", "i wasn't born again yesterday", "a track"));         my_list.add(new sample_model("faith + 1", "pleasing jesus", "a track"));         my_list.add(new sample_model("faith + 1", "jesus (looks kinda hot)", "a track"));         my_list.add(new sample_model("butters", "what what", "b track"));         return my_list;     }      public observablecollection<sample_model> datasource { get; set; }      sample_model _seleteditem;     public sample_model selecteditem     {         { return _seleteditem; }         set         {             _seleteditem = value;             notifypropertychanged("selecteditem");         }     }  } 

<grid>     <grid.columndefinitions>         <columndefinition width="217*"/>         <columndefinition width="300*"/>     </grid.columndefinitions>     <listview x:name="mylistview" width="200" selectionchanged="mylistview_selectionchanged" horizontalalignment="left"               selecteditem="{binding selecteditem, mode=twoway, updatesourcetrigger=propertychanged}">         <listview.itemtemplate>             <datatemplate>                 <stackpanel>                     <textblock text="{binding artist}"></textblock>                     <textblock text="{binding song}"></textblock>                 </stackpanel>             </datatemplate>         </listview.itemtemplate>     </listview>     <button x:name="mybutton" grid.column="1" content="change selected item" click="mybutton_click"></button> </grid> 

public partial class mainwindow : window {     private sample_viewmodel viewmodel;     public mainwindow()     {         initializecomponent();     }      private void window_loaded(object sender, routedeventargs e)     {         sample_viewmodel viewmodel = new sample_viewmodel();  // create view model         mylistview.datacontext = viewmodel;                   // set datacontext (this link commands)         mylistview.itemssource = viewmodel.datasource;        // set itemssource, link artist,songs     }      private void mylistview_selectionchanged(object sender, selectionchangedeventargs e)     {     }      private void mybutton_click(object sender, routedeventargs e)     {         // testing purpose, don't use code         mylistview.selecteditem = (sample_model) ((observablecollection<sample_model>)mylistview.itemssource)[2];          // or can         // viewmodel.selecteditem = (sample_model)((observablecollection<sample_model>)mylistview.itemssource)[2];          // or         // viewmodel.selecteditem = viewmodel.datasource[2];          mylistview.focus();                 } } 

enter image description here


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -