c# - How to change selected item of listbox when clicking on the Button / textbox in ItemTemplate -


in wpf project have listbox itemssource bound set of items. use datatemplate in itemtemplate of listbox represent ui of these items.

what want happen when user clicks any part of datatemplate bound item listbox.selecteditem set item datatemplate is. selected style applied. can see sample code below, clicking on label fine. however, controls such button , textbox not behave wanted , there no doubt others too. suspect it's got focus?

can how might achieve please?

many in advance help.

xaml:

<window x:class="listviewselectionoverride.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:listviewselectionoverride"     mc:ignorable="d"     title="mainwindow" height="350" width="525"> <grid>      <listbox itemssource="{binding}">          <listbox.resources>             <style targettype="listboxitem">                 <setter property="snapstodevicepixels" value="true" />                 <setter property="overridesdefaultstyle" value="true" />                 <setter property="template">                     <setter.value>                         <controltemplate targettype="listboxitem">                             <border                     name="border"                     padding="2"                     snapstodevicepixels="true">                                 <contentpresenter />                             </border>                             <controltemplate.triggers>                                 <trigger property="isselected" value="true">                                     <setter targetname="border" property="borderbrush" value="blue"/>                                     <setter targetname="border" property="borderthickness" value="1"/>                                     <setter property="fontweight" value="bold" />                                     <setter property="foreground" value="black" />                                 </trigger>                             </controltemplate.triggers>                         </controltemplate>                     </setter.value>                 </setter>             </style>         </listbox.resources>          <listbox.itemspanel>             <itemspaneltemplate>                 <wrappanel/>             </itemspaneltemplate>         </listbox.itemspanel>          <listbox.itemtemplate>             <datatemplate>                 <border x:name="itemtemplateborder">                     <grid >                         <grid.rowdefinitions>                             <rowdefinition/>                             <rowdefinition/>                             <rowdefinition/>                             <rowdefinition/>                         </grid.rowdefinitions>                          <label margin="3,3,3,0" grid.row="0" content="label"/>                         <button margin="3,3,3,0" grid.row="1" content="button"/>                         <textbox margin="3,3,3,0" grid.row="2" text="textbox"/>                         <checkbox margin="3,3,3,0" grid.row="3" content="checkbox"/>                     </grid>                 </border>             </datatemplate>         </listbox.itemtemplate>     </listbox> </grid> 

code behind:

public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();          // dummy items generate 4 items in listbox         datacontext = new object[] { 1, 2, 3, 4 };     } } 

you handle previewmouseleftbuttondown event border element in template , explicitly select corresponding item:

private void border_previewmouseleftbuttondown(object sender, mousebuttoneventargs e) {     border border = sender border;     lvv.selecteditem = border.datacontext; } 

<setter property="template">     <setter.value>         <controltemplate targettype="listboxitem">             <border name="border" padding="2" snapstodevicepixels="true" previewmouseleftbuttondown="border_previewmouseleftbuttondown">                 <contentpresenter />             </border>             <controltemplate.triggers>                 <trigger property="isselected" value="true">                     <setter targetname="border" property="borderbrush" value="blue"/>                     <setter targetname="border" property="borderthickness" value="1"/>                     <setter property="fontweight" value="bold" />                     <setter property="foreground" value="black" />                 </trigger>             </controltemplate.triggers>         </controltemplate>     </setter.value> </setter> 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -