c# - Bind more than one ViewModel in View in XAML -
i have viewmodel class "myviewmodel" , within have observablecollection "mycollection".
then in view code-behind set info context:
this.datacontext = new myviewmodel();
and in front-end of view
<pivot itemssource="{binding mycollection}" selectionchanged="pivot_selectionchanged" margin="0" grid.row="1">
but if need utilize different viewmodel "myviewmodel2" "mycollection2" within in view. how can skip step:
this.datacontext = new myviewmodel();
and bind in xaml?
i tried without result:
<pivot itemssource="{binding myviewmodel.mycollection}" selectionchanged="pivot_selectionchanged" margin="0" grid.row="1">
you'd improve utilize 2 collections in same view model... or can utilize master viewmodel class reference 2 sub-viewmodels; this:
myviewmodel class:
public class myviewmodel { public observablecollection<string> datainfo { get; set; } public myviewmodel() { datainfo = new observablecollection<string>(); } }
masterviewmodel class:
public class masterviewmodel { public myviewmodel vm1 { get; set; } public myviewmodel vm2 { get; set; } public masterviewmodel() { this.vm1 = new myviewmodel(); this.vm2 = new myviewmodel(); vm1.datainfo.add("data 1-1"); vm1.datainfo.add("data 1-2"); vm2.datainfo.add("data 2-1"); vm2.datainfo.add("data 2-2"); } }
view code-behind:
public partial class mainwindow : window { public mainwindow() { initializecomponent(); this.datacontext = new masterviewmodel(); } }
xaml
<window x:class="deleteme4sow.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="400" width="525"> <stackpanel orientation="vertical"> <listview itemssource="{binding vm1.datainfo}" height="150" margin="10" /> <listview itemssource="{binding vm2.datainfo}" height="150" margin="10"/> </stackpanel> </window>
c# wpf xaml mvvm windows-phone
No comments:
Post a Comment