c# - Button command(s) to select next / previous item in ListBox -


is there simple command can send standard wpf listbox using button select next / previous item in list?

currently i'm rolling solution:

            <button width="30" height="30" x:name="previousbutton">                 <i:interaction.triggers>                     <i:eventtrigger eventname="click">                         <ei:changepropertyaction increment="true"                                                  propertyname="selectedindex"                                                  targetname="mylistbox"                                                  value="-1" />                     </i:eventtrigger>                 </i:interaction.triggers>             </button>            <!-- listbox here. -->            <button width="30" height="30" x:name="nextbutton">                 <i:interaction.triggers>                     <i:eventtrigger eventname="click">                         <ei:changepropertyaction increment="true"                                                  propertyname="selectedindex"                                                  targetname="mylistbox"                                                  value="1" />                     </i:eventtrigger>                 </i:interaction.triggers>             </button> 

which fine. cause exception if hit previous button when you're on first item on list (same last), current plan tell disable button if selectedindex first/last item in list.

i'm asking question see if i'm missing trick more anything. "no, that's not possible, have other way" acceptable answer if that's case.

using mvvm nice , simpler. how solve it.note sample code uses mvvmlight toolkit.

view

<window x:class="stackoverflow1.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"         xmlns:local="clr-namespace:stackoverflow1"         xmlns:viewmodel="clr-namespace:stackoverflow1.viewmodel"         mc:ignorable="d"         title="mainwindow" height="350" width="525">      <window.resources>         <viewmodel:mainviewmodel x:key="mainviewmodel"/>     </window.resources>     <grid datacontext="{staticresource mainviewmodel}">         <grid.rowdefinitions>             <rowdefinition height="*"/>             <rowdefinition height="auto"/>         </grid.rowdefinitions>          <listbox grid.row="0" horizontalalignment="stretch" verticalalignment="stretch" itemssource="{binding students}" x:name="listbox"                  selectedindex="{binding selectedindex}"/>         <stackpanel orientation="horizontal" grid.row="1">             <button content="next" width="50" height="24" command="{binding nextcommand}" commandparameter="{binding elementname=listbox,path=selectedindex}"/>             <button content="previous" width="50" height="24" command="{binding previouscommand}" commandparameter="{binding elementname=listbox,path=selectedindex}"/>         </stackpanel>     </grid> </window> 

viewmodel

using system.collections.generic; using system.collections.objectmodel; using system.windows.input; using galasoft.mvvmlight; using galasoft.mvvmlight.commandwpf;  namespace stackoverflow1.viewmodel {      public class mainviewmodel : viewmodelbase     {          public mainviewmodel()         {          nextcommand=new relaycommand<int>(onnext,cannext);          previouscommand=new relaycommand<int>(onprevious,canprevious);             selectedindex = 0;              foreach (var student in getstudent())             {                 _students.add(student);             }         }          public icommand nextcommand { get; set; }         public icommand previouscommand { get; set; }         private int _selectedindex;           private list<student> getstudent()         {             return new list<student>()             {                 new student {id = 0,name = "kwame0"},                  new student {id = 0,name = "kwame1"},                  new student {id = 0,name = "kwame2"},                  new student {id = 0,name = "kwame3"},                   new student {id = 0,name = "kwame4"},                  new student {id = 0,name = "kwame5"},                  new student {id = 0,name = "kwame6"},                  new student {id = 0,name = "kwame7"},                    new student {id = 0,name = "kwame8"},                  new student {id = 0,name = "kwame9"},                  new student {id = 0,name = "kwame10"},                  new student {id = 0,name = "kwame11"},                   new student {id = 0,name = "kwame12"},                  new student {id = 0,name = "kwame13"},                  new student {id = 0,name = "kwame14"},                  new student {id = 0,name = "kwame15"},             };         }         public int selectedindex         {             { return _selectedindex; }             set { _selectedindex = value;raisepropertychanged(()=>selectedindex); }         }           private observablecollection<student> _students=new observablecollection<student>();          public observablecollection<student> students         {             { return _students; }             set { _students = value; }         }           private void onnext(int index)         {             if (selectedindex != students.count)                 selectedindex += 1;             else             {                 selectedindex = 0;             }         }          private bool cannext(int indext)         {             return selectedindex != students.count;         }          private void onprevious(int index)         {             if (selectedindex != 0)                 selectedindex -= 1;             else             {                 selectedindex = students.count;             }         }          private bool canprevious(int index)         {             return selectedindex != 0;         }      }      public class student     {         public int id { get; set; }         public string name { get; set; }          public override string tostring()         {             return $"{id}-{name}";         }     } } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -