c# - WPF HandleListBoxClickEvent works only on the empty space of the list -


why handlelistboxclickevent works on empty space of list? problem when want refresh list. while clicking on item - nothing updating. clicking on empty space of list (when item selected) - selected item updating.

i think, problem should in xaml, because following ready (working) example c# code.

resources:

    <usercontrol.resources>     <style x:key="redglowitemcontainer" targettype="{x:type listboxitem}">         <setter property="control.template">             <setter.value>                 <controltemplate targettype="{x:type listboxitem}">                     <border name="iconborder" background="#00ffffff">                         <contentpresenter />                     </border>                     <controltemplate.triggers>                         <trigger property="listboxitem.isselected" value="true">                             <setter targetname="iconborder" property="border.background" value="#ff07a3e9" />                             <setter property="control.fontweight" value="bold" />                             <setter property="control.foreground" value="#ffffffff" />                         </trigger>                     </controltemplate.triggers>                 </controltemplate>             </setter.value>         </setter>     </style>     <datatemplate x:key="categorytemplate" datatype="{x:type vm:orderentryviewmodel}">         <vw:menuitemselectorview />     </datatemplate>     <mmc:ordertemplateselector x:key="paneltemplateselector" /> </usercontrol.resources> 

viewmodel:

internal class orderentryviewmodel : viewmodelbase, iparentviewmodel, iviewmodelbase { ... public void handlelistboxclickevent()     {         if (this._selectedorder != null)         {             if (this._selectedorder.isnew)             {                 if (this._qty != "")                 {                     this._selectedorder.quantity = int.parse(this._qty);                     this.selectedquantity = "";                     this.calculatetotal();                 }             }         } } ... } 

xaml.cs:

public partial class orderentryview : system.windows.controls.usercontrol {     private bool isexpended = true;      public orderentryview()     {         this.initializecomponent();     }      private void lstticket_mousedown(object sender, mousebuttoneventargs e)     {         ((orderentryviewmodel)base.datacontext).handlelistboxclickevent();     }      private void usercontrol_loaded(object sender, routedeventargs e)     {     } } 

xaml list:

 <listbox name="lstticket" issynchronizedwithcurrentitem="true" verticalalignment="stretch" scrollviewer.horizontalscrollbarvisibility="hidden" scrollviewer.verticalscrollbarvisibility="hidden" itemcontainerstyle="{staticresource redglowitemcontainer}" fontsize="12" itemssource="{binding orders, mode=twoway}" selecteditem="{binding selectedorder, mode=twoway}" common:listboxextenders.autoscrolltoend="true" mousedown="lstticket_mousedown" grid.row="1">             <listbox.itemtemplate>                 <datatemplate>                     <stackpanel horizontalalignment="stretch" background="#00ffffff">                         <grid background="#00ffffff" width="230">                             <grid.columndefinitions>                                 <columndefinition width="24" />                                 <columndefinition width="*" />                                 <columndefinition width="45" />                             </grid.columndefinitions>                             <grid.rowdefinitions>                                 <rowdefinition height="25" />                             </grid.rowdefinitions>                             <textblock style="{dynamicresource stylestrikethrough}" grid.column="0" width="24" padding="0" horizontalalignment="left" text="{binding quantity, stringformat=0}" />                             <textblock style="{dynamicresource stylestrikethrough}" grid.column="1" horizontalalignment="left" padding="0" text="{binding displayname}" />                             <textblock style="{dynamicresource stylestrikethrough}" grid.column="2" padding="0" horizontalalignment="right" text="{binding totalprice, stringformat=n2}" />                         </grid>                         <itemscontrol itemssource="{binding ordermodifiers}">                             <itemscontrol.itemtemplate>                                 <datatemplate>                                     <grid background="#00ffffff">                                         <label padding="0" foreground="#ff0000ff" margin="30,0,0,0" horizontalalignment="left" content="{binding displayname}" />                                         <label padding="0" foreground="#ff0000ff" margin="0,0,20,0" horizontalalignment="right" content="{binding price, stringformat=n2}" />                                     </grid>                                 </datatemplate>                             </itemscontrol.itemtemplate>                         </itemscontrol>                         <itemscontrol itemssource="{binding ordersetitems}">                             <itemscontrol.itemtemplate>                                 <datatemplate>                                     <stackpanel>                                         <grid background="#00ffffff" width="230">                                             <grid.columndefinitions>                                                 <columndefinition width="24" />                                                 <columndefinition width="*" />                                                 <columndefinition width="45" />                                             </grid.columndefinitions>                                             <grid.rowdefinitions>                                                 <rowdefinition height="15" />                                             </grid.rowdefinitions>                                             <textblock style="{dynamicresource stylestrikethrough}" margin="10,0,0,0" grid.column="0" width="24" padding="0" horizontalalignment="left" text="{binding quantity, stringformat=0}" />                                             <textblock style="{dynamicresource stylestrikethrough}" grid.column="1" horizontalalignment="left" padding="0" text="{binding displayname}" />                                             <textblock style="{dynamicresource stylestrikethrough}" grid.column="2" padding="0" horizontalalignment="right" text="{binding totalprice, stringformat=n2}" />                                         </grid>                                         <itemscontrol itemssource="{binding ordersetmodifiers}">                                             <itemscontrol.itemtemplate>                                                 <datatemplate>                                                     <grid background="#00ffffff">                                                         <label padding="0" foreground="#ff0000ff" margin="30,0,0,0" horizontalalignment="left" content="{binding displayname}" />                                                         <label padding="0" foreground="#ff0000ff" margin="0,0,20,0" horizontalalignment="right" content="{binding price}" />                                                     </grid>                                                 </datatemplate>                                             </itemscontrol.itemtemplate>                                         </itemscontrol>                                     </stackpanel>                                 </datatemplate>                             </itemscontrol.itemtemplate>                         </itemscontrol>                     </stackpanel>                 </datatemplate>             </listbox.itemtemplate>         </listbox> 

without a good, minimal, complete code example illustrates question, it's not practical try provide working code example.

but can tell basic issue is: when user clicks in actual item in listbox, item handles user input.

you can either preview mouse event (see e.g. mouse.previewmousedown attached event, paying close attention documented caveats), or can handle mousedown event in list item itself. see e.g. how can event or command fire when user clicks on listviewitem? more information regarding that.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -