c# - How to manage User Control in List Box using MVVM? -
i know there many question of this, don't find correct answer, or don't understand correct way solve. have list box in mainwindows, populated custom object (fooobjclass).
<listbox x:name="foolistbox"> <listbox.itemtemplate> <datatemplate> <foonamespace:fooobjview/> </datatemplate> </listbox.itemtemplate> </listbox>
the fooobjview user control
<usercontrol x:class="plcs7_test.smartobjrecognize.smartobjview" 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:test.fooobjrecognize" mc:ignorable="d" width="auto"> <usercontrol.datacontext> <local:fooviewmodel/> </usercontrol.datacontext> <grid> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="auto"/> </grid.columndefinitions> <textblock grid.column="0" text="{binding path=fooobjprop.name}"/> <textblock grid.column="1" text="{binding path=fooobjprop.type}"> </grid>
and fooviewmodel
class fooviewmodel : observableobject { private fooobjclass fooobjmember; public fooobjclass fooobjprop { { return fooobjmember; } set { this.fooobjmember= value; base.raisepropertychanged("fooobjprop"); } } }
the fooobjclass normal class , observableobject class this:
public abstract class observableobject : inotifypropertychanged { public event propertychangedeventhandler propertychanged; protected virtual void onpropertychanged(propertychangedeventargs e) { var handler = this.propertychanged; if (handler != null) { handler(this, e); } } protected void raisepropertychanged<t>(expression<func<t>> propertyexpresssion) { var propertyname = propertysupport.extractpropertyname(propertyexpresssion); this.raisepropertychanged(propertyname); } protected void raisepropertychanged(string propertyname) { verifypropertyname(propertyname); onpropertychanged(new propertychangedeventargs(propertyname)); } public void verifypropertyname(string propertyname) { // verify property name matches real, // public, instance property on object. if (typedescriptor.getproperties(this)[propertyname] == null) { debug.fail("invalid property name: " + propertyname); } } }
now, can pass item object of list box (it's fooobjclass ) last foomodelview? have use dependency property? how? tryed , readed all, don't find solution
with piece of code pass item object of list box fooviewmodel
<usercontrol.datacontext> <local:fooviewmodel fooobjmember="{databinding}"/> </usercontrol.datacontext>
but fooobjmember not dependency property, , when tried create dependency property it's same
thank , sorry english ;)
welcome,
here errors:
- you're not specifying binding listboxitem
- you instantiating model in usercontrol
- you binding field
fooobjmember
, properties allowed
here's working example:
window xaml
<window x:class="wpfapplication1.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:wpfapplication1" mc:ignorable="d" title="mainwindow" height="350" width="525"> <grid> <!--<listbox itemssource="{binding}"> binds datacontext property--> <listbox itemssource="{binding}"> <listbox.itemtemplate> <datatemplate> <local:myusercontrol datacontext="{binding}" /> </datatemplate> </listbox.itemtemplate> </listbox> </grid> </window>
window code
using system.collections.generic; namespace wpfapplication1 { public partial class mainwindow { public mainwindow() { initializecomponent(); datacontext = new list<mymodel> { new mymodel {number = 1, value = "one"}, new mymodel {number = 2, value = "two"} }; } } }
user control xaml
<usercontrol x:class="wpfapplication1.myusercontrol" 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" mc:ignorable="d" d:datacontext="{d:designinstance local:mymodel}" d:designheight="300" d:designwidth="300"> <grid> <border padding="5" borderbrush="red" borderthickness="1"> <stackpanel> <textblock text="{binding number}" /> <textblock text="{binding value}" /> </stackpanel> </border> </grid> </usercontrol>
user control code
namespace wpfapplication1 { public partial class myusercontrol { public myusercontrol() { initializecomponent(); } } }
model code
using system.componentmodel; using system.runtime.compilerservices; namespace wpfapplication1 { internal class mymodel : inotifypropertychanged { private int _number; private string _value; public int number { { return _number; } set { if (value == _number) return; _number = value; onpropertychanged(); } } public string value { { return _value; } set { if (value == _value) return; _value = value; onpropertychanged(); } } public event propertychangedeventhandler propertychanged; protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } } }
result
now adjust needs :d
Comments
Post a Comment