c# - Why is my WPF listview code throwing an excpetion when programatically selecting a row -


i trying programmatically highlight first row in wpf listview control using vs2008 3.5 of .net framework. here c# code this:

listviewitem val = (listviewitem)listview1.items[0]; val.isselected = true; 

the code throws exception @ first line, after listview1 populated data. message in exception says:

"unable cast object of type 'message.lv1data' type 'system.windows.controls.listviewitem'."

lv1data class using bind columns in control. so, looks trying return lv1data object instead of listviewitem object. have suggestions doing wrong or need in order programmatically highlight listview row?

here xaml code listview control:

<listview x:name="listview1" itemcontainerstyle="{staticresource alternatinglistviewitemstyle}" alternationcount="2" selectionchanged="listview1_selectionchanged"  selectionmode="multiple" horizontalalignment="left" itemssource = "{binding elementname=lobbywindow, path=listcollection1}">    <listview.view>       <gridview>          <gridviewcolumn displaymemberbinding="{binding game}">          <gridviewcolumnheader content="game" fontweight="bold" />          </gridviewcolumn>          <gridviewcolumn displaymemberbinding="{binding stakes}">          <gridviewcolumnheader content="stakes" width="68" fontweight="bold" />          </gridviewcolumn>          <gridviewcolumn width="30" displaymemberbinding="{binding seats}">          <gridviewcolumnheader content="seats" fontweight="bold" />          </gridviewcolumn>       </gridview>    </listview.view> </listview> 

edit

<!-- define resource alternating background background used in listview objects.  --> <stackpanel.resources>    <style x:key="alternatinglistviewitemstyle" targettype="{x:type listviewitem}">       <setter property="isselected" value="{binding isselected}"/>       <style.resources>          <!-- foreground selected listviewitem -->          <!-- <solidcolorbrush x:key="{x:static systemcolors.highlighttextbrushkey}" color="black"/> -->          <!-- background selected listviewitem -->          <solidcolorbrush x:key="{x:static systemcolors.highlightbrushkey}" color="green"/>          <solidcolorbrush x:key="{x:static systemcolors.controlbrushkey}" color="brown"/>       </style.resources>       <style.triggers>          <!-- setting triggers alternate background colors -->          <trigger property="itemscontrol.alternationindex" value="1">             <setter property="background" value="#ffd9f2bf"></setter>          </trigger>          <trigger property="itemscontrol.alternationindex" value="2">             <setter property="background" value="white"></setter>          </trigger>          <trigger property="isselected" value="true">             <setter property="background" value="lightblue"></setter>          </trigger>          <trigger property="ismouseover" value="true">             <setter property="borderbrush" value="lightblue" />          </trigger>          <multitrigger>             <multitrigger.conditions>                <condition property="isselected" value="true"></condition>                <condition property="itemscontrol.alternationindex" value="0"></condition>             </multitrigger.conditions>             <setter property="background" value="lightblue"></setter>          </multitrigger>          <multitrigger>             <multitrigger.conditions>                <condition property="isselected" value="true"></condition>                <condition property="itemscontrol.alternationindex" value="1"></condition>             </multitrigger.conditions>             <setter property="background" value="lightgreen"></setter>          </multitrigger>       </style.triggers>       <!-- setting row height here -->    </style> </stackpanel.resources> 

you bound items source, means asking items[x] return type of data bound (whatever type stored in listcollection1).

if want alter it's isselected, you'll have create property on type in listcollection1, , bind in style or template.

the isselected property create have implemented dependencyproperty, or type it's in have implement inotifypropertychanged, , trigger event when property changes.

<listview itemssource="...">    <listview.itemcontainerstyle>       <style targettype="{x:type listviewitem}">          <setter property="isselected" value="{binding isselected}"/>       </style>    </listview.itemcontainerstyle> </listview> 

then you'll cast dataobject type, , set it's isselected value.

find in code listcollection1. it's definition list<element>. element type need cast to.

element needs either like

public class element : inotifypropertychanged {    private _isselected;    public boolean isselected     {        { return _isselected; }       set        {            _isselected = value;            if (propertychanged != null)               propertychanged("isselected");       }    }         //snip implement interface inotifypropertychanged.     //snip other code } 

-or-

public class element : dependencyobject {     public static dependencyproperty isselectedproperty =        dependencyproperty.register("isselected" ...     //snip other code } 

then code should this.

element val = (element)listview1.items[0]; val.isselected = true; 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -