c# - ListBox not correctly changing ContentControl's ViewModel -
i'm still pretty new wpf , decided change application developing start following mvvm pattern best could. running problem when try have list box dictate view model of content control. i've been stuck on while , searching internet not producing answers me.
for reason new instance of view model list box contains being generated data context of content control. when debugging made sure list box contains view models should, , item select on list box indeed item list box selecting, content control changing based on selection. there view model populating content control, not in collection list box populates from. , can somehow delete view model in content control via remove button. when make selection change on list box, or add new item collection populates content control new view model once again not in collection. have no clue why doing this, or in code suggest behavior.
i made simple application try , figure out i'm doing wrong. replicates problem perfectly. i'm pretty sure buttons don't adhere mvvvm (supposed run command contained in view model adhere mvvm i've been reading) not main concern right problem exists without buttons.
mainwindow.xml
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfapplication1" x:class="wpfapplication1.mainwindow" title="mainwindow" height="440" width="436"> <window.datacontext> <local:mwvm/> </window.datacontext> <window.resources> <datatemplate datatype="{x:type local:ucvm}"> <local:uc/> </datatemplate> </window.resources> <grid> <button content="a" horizontalalignment="left" margin="323,351,0,0" verticalalignment="top" width="95" click="button_click"/> <button content="r" horizontalalignment="left" margin="323,378,0,0" verticalalignment="top" width="95" click="button_click_1"/> <contentcontrol margin="10,10,110,10" content="{binding selecteditem, elementname=lb_ucs}"/> <listbox x:name="lb_ucs" horizontalalignment="left" height="336" margin="323,10,0,0" verticalalignment="top" width="95" itemssource="{binding ucs}" displaymemberpath="cooltext"/> </grid> </window>
mainwindow.xaml.cs
public partial class panelpartsview : usercontrol { private panelpartsviewmodel _dc; public panelpartsview() { initializecomponent(); _dc = datacontext panelpartsviewmodel; } private void btn_remove_click(object sender, routedeventargs e) { _dc.panels.remove(lb_panels.selecteditem partsviewmodel); } private void btn_add_click(object sender, routedeventargs e) { var pvm = new partsviewmodel(); _dc.panels.add(pvm); lb_panels.selecteditem = pvm; system.console.writeline("lb_panels.selecteditem = {0}", ((partsviewmodel)lb_panels.selecteditem).panelname); system.console.writeline("cc_panelparts.content = {0}", ((partsviewmodel)cc_panelparts.content).panelname); } }
mwvm
class mwvm { private observablecollection<ucvm> _ucs = new observablecollection<ucvm>(); public observablecollection<ucvm> ucs { { return _ucs; } } public mwvm() { //this for testing, real application purely dynamic _ucs.add(new ucvm()); _ucs.add(new ucvm()); _ucs.add(new ucvm()); } }
uc.xaml
<usercontrol xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:wpfapplication1" x:class="wpfapplication1.uc" mc:ignorable="d" d:designwidth="300" height="90"> <grid> <grid.datacontext> <local:ucvm/> </grid.datacontext> <button content="{binding cooltext}" margin="10,10,10,0" height="44" verticalalignment="top"/> <textbox height="23" margin="10,59,10,0" textwrapping="wrap" text="{binding cooltext}" verticalalignment="top"/> </grid> </usercontrol>
uc.xaml.cs
public partial class uc : usercontrol { public uc() { initializecomponent(); } }
ucvm.cs
class ucvm : inotifypropertychanged { private static int = 1; private string _cooltext = "<" + i++ + ">" + system.datetime.now.tolongtimestring(); public string cooltext { { return _cooltext; } set { _cooltext = value; npc("cooltext"); } } public event propertychangedeventhandler propertychanged; private void npc(string s) { if (propertychanged != null) propertychanged(this, new propertychangedeventargs(s)); } }
i have tried binding content control so...
<contentcontrol content="{binding selecteducvml, mode=oneway}"/> <listbox x:name="lb_ucs" itemssource="{binding ucs}" selecteditem="{binding selecteducvm}" displaymemberpath="cooltext"/>
...and so...
<contentcontrol content="{binding ucs/}"/> <listbox x:name="lb_ucs" itemssource="{binding ucs}" issynchronizedwithcurrentitem="true" displaymemberpath="cooltext"/>
but no avail.
any appreciated.
it looks have remove part uc.xaml:
<grid.datacontext> <local:ucvm/> </grid.datacontext>
this syntax creates new instance of view model, each time instance of uc.xaml created, of course isn't want. want data context of uc.xaml instances inherit instance selected in list box.
Comments
Post a Comment