wpf - Getting weird behaviours when changing the binding to the TreeView -
i have observablecollection fathers
contains property observablecollection sons
. , i'm displaying on treeview
setting datacontext
property. sons
property displays listbox
of radio button under each father - binded itemssource
.
first time setting datacontext of tree view fathers list, working good. radio buttons checked according data.
now, i'm setting treeview.datacontext
null - data disappear. , original fathers
observablecollection set in first time.
and reason radio buttons stopped being synchronized son object. , got deeper , saw setter in son object (that binded radio button) raised false reason. guess related binding.
is there cache treeview
, or observablecollection saving after binding ? want work first time set bind - there should getter being called should.
thanks.
this tree view
<usercontrol x:class="tester.ctlmytree" 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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" mc:ignorable="d" d:designheight="300" d:designwidth="400"> <grid x:name="layoutroot" background="white"> <border background="#ff919191" borderthickness="1" cornerradius="5" horizontalalignment="left" verticalalignment="top" padding="5" borderbrush="black" height="207" width="190"> <border.resources> <sdk:hierarchicaldatatemplate x:key="layerlisttemplate"> <stackpanel orientation="vertical" width="200" > <textblock text="hello"/> <listbox x:name="lstviews" itemssource="{binding sons}" borderthickness="0" width="200"> <listbox.itemtemplate> <datatemplate> <radiobutton content="check" ischecked="{binding ischecked, mode=twoway}"/> </datatemplate> </listbox.itemtemplate> </listbox> </stackpanel> </sdk:hierarchicaldatatemplate> </border.resources> <sdk:treeview itemssource="{binding}" itemtemplate="{staticresource layerlisttemplate}" x:name="mytreeview" /> </border> </grid> </usercontrol>
the objects behind
public class ccfather { public ccfather() { sons = new observablecollection<ccson>(); } public observablecollection<ccson> sons { get; set; } } public class ccson { private bool m_blnchecked; public bool ischecked { { return m_blnchecked; } set { m_blnchecked = value; } } }
in application added treeview control , called m_objsimpletree. code initializing
m_objitems = new observablecollection<ccfather>(); ccfather objitem1 = new ccfather(); objitem1.sons.add(new ccson()); objitem1.sons[0].ischecked = true; m_objitems.add(objitem1); m_objsimpletree.mytreeview.datacontext = m_objitems;
and when press button i'm doing this
m_objsimpletree.mytreeview.datacontext = null; m_objsimpletree.mytreeview.datacontext = m_objitems;
this code raise ischecked setter of son false (why ???) radiobutton still checked. second time pressing button. unchecked , setter didn't raise. when i'm pressing on radio button it's raising twice setter. first time false second true.
can't figure why it's happening.. think can think of treeview saving in first binding or this.
it because have used twoway binding control
in 2 way binding when change thig on view data gets saved in object . datacontext assigned. try oneway . need careful if wanna save data using twoway 1 way might not help. mvvm suggests use 2 way binding save data want refreshed list create new object :)
also try clear binding
am not sure of last link never tried. please o through might idea. since might need new object again can create fresh object assign datacontext.
--- edit-----
here's xaml code
<usercontrol 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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:class="silverlightsoapp.mainpage" mc:ignorable="d" d:designheight="300" d:designwidth="400"> <grid x:name="layoutroot" background="white"> <border background="#ff919191" borderthickness="1" cornerradius="5" horizontalalignment="left" verticalalignment="top" padding="5" borderbrush="black" height="207" width="190"> <border.resources> <sdk:hierarchicaldatatemplate x:key="layerlisttemplate"> <stackpanel orientation="vertical" width="200" > <textblock text="hello"/> <listbox x:name="lstviews" itemssource="{binding sons}" borderthickness="0" width="200"> <listbox.itemtemplate> <datatemplate> <stackpanel> <radiobutton content="check" groupname="abcd" ischecked="{binding ischecked, mode=twoway}"/> <radiobutton content="check" groupname="abcd" ischecked="{binding ischecked2, mode=twoway}"/> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox> </stackpanel> </sdk:hierarchicaldatatemplate> </border.resources> <sdk:treeview itemssource="{binding}" itemtemplate="{staticresource layerlisttemplate}" x:name="mytreeview" /> </border> <button content="button" horizontalalignment="left" margin="303,268,0,0" verticalalignment="top" width="75" click="button_click"/> </grid>
and c#
using system;
using system.collections.generic; using system.collections.objectmodel; using system.linq; using system.net; using system.windows; using system.windows.browser; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes;
namespace silverlightsoapp { public partial class mainpage : usercontrol {
private observablecollection<ccfather> m_objitems; public mainpage() { initializecomponent(); loaded += new routedeventhandler(mainpage_loaded); } void mainpage_loaded(object sender, routedeventargs e) { m_objitems = new observablecollection<ccfather>(); ccfather objitem1 = new ccfather(); objitem1.sons.add(new ccson()); objitem1.sons[0].ischecked = false; m_objitems.add(objitem1); mytreeview.datacontext = m_objitems; } private void button_click(object sender, routedeventargs e) { mytreeview.datacontext = null; mytreeview.datacontext = m_objitems; } } public class ccfather { public ccfather() { sons = new observablecollection<ccson>(); } public observablecollection<ccson> sons { get; set; } } public class ccson { private bool m_blnchecked; private bool m_blnchecked2; public bool ischecked { { return m_blnchecked; } set { m_blnchecked = value; } } public bool ischecked2 { { return m_blnchecked2; } set { m_blnchecked2 = value; } } }
}
now main point if want implement single radio button need implement click event , set radio button false , next time true :) or else need use checkbox
1 radio button once checked cannot converted false
Comments
Post a Comment