wpf - Why are my TreeViewItems acting like RadioButtons? -


i have wpf treeview i've implemented small model class behind scenes. bind list of them treeview's itemssource when creating control. (i've pared code here down bit sake of simplicity, should reproducable.)

public class treeviewitemmodel {     public observablecollection<treeviewitemmodel> children { get; set; }     public string name { get; set; }     public bool isselected { get; set; }      public treeviewitemmodel()     {         children = new observablecollection<treeviewitemmodel>();         isselected = false;     } }  public partial class mainwindow : window {     public observablecollection<treeviewitemmodel> mytree { get; set; }      public mainwindow()     {         initializecomponent();          // add dummy values         list<treeviewitemmodel> items = new list<treeviewitemmodel>();         (int = 0; < 10; i++)    items.add(new treeviewitemmodel() { name = ("node" + i)   });         mytree = new observablecollection<treeviewitemmodel>(items);          datacontext = this;     } } 

my treeviewitems contain checkboxes. now, i'd like bind isselected checkbox @ end of day (hopefully) have list of treeviewitemmodel classes isselected set whether or not checkbox checked.

to end, have style:

<style x:key="{x:type treeviewitem}" targettype="{x:type treeviewitem}">     <setter property="isselected" value="{binding isselected, mode=twoway}" /> </style> 

and treeview declaration:

<treeview itemssource="{binding mytree}" >     <treeview.resources>             <datatemplate datatype="{x:type ui:treeviewitemmodel}">             <stackpanel orientation="horizontal">                 <checkbox content="{binding name}" ischecked="{binding isselected}" />             </stackpanel>         </datatemplate>     </treeview.resources> </treeview> 

this almost works. can create list of items programmatically , bound treeview, check off items in treeview, , when check them in c# isselected set appropriately.

except 1 thing: treeviewitems act radiobuttons. click one, , sets isselected true. rejoice! click on another... , deselects first treeviewitem! can never have more 1 selected @ time.

but... why?! don't understand @ all. they're bound different items on backend, why setting isselected change state of item?

:'(

in style treeviewitem bind treeviewitem.isselected isselected property of view model means checkbox checked if treeviewitem selected. happens because wpf treeview not support multi selection.

you can add multi selection changing treeviewitem content checkbox or togglebutton, you're trying achieve, cannot bind treeviewitem.isselected view model. happens is

  • you click select 1 item
  • previous treeviewitem.isselected set false
  • this passed view model isselected
  • which passed checkbox.ischecked
  • new treeviewitem.isselected set true
  • and on

remove style treeviewitem , leave checkbox.ischecked isselected binding

on side note don't need stackpanel when want show 1 element checkbox


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -