Tuesday 15 March 2011

c# - Use a ListView as a Navigation bar in Wpf -



c# - Use a ListView as a Navigation bar in Wpf -

my application has grid 2 columns. in left column, have listview, items info binding. on right want implement view display content based on item selected on listview. how do that?

<window x:class="wpfapplication8.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525" background="lightblue"> <grid name="maingrid"> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="3*" /> </grid.columndefinitions> <listview grid.column="0" name="listview" itemssource="{binding path=clientcollection}" > <listview.itemtemplate> <datatemplate> <textblock text="{binding path=clientname}" /> </datatemplate> </listview.itemtemplate> </listview> </grid>

i believe best way accomplish add together selecteditem property viewmodel, witch binded selected item of listview, , create property datacontext of right view; here illustration :

<grid> <grid.columndefinitions> <columndefinition width="*"/> <columndefinition width="*"/> </grid.columndefinitions> <listview grid.column="0" x:name="lvlistview" itemssource="{binding clientcollection}" selecteditem="{binding selectedclient}" > <listview.view> <gridview> <gridviewcolumn width="150" header="name" displaymemberbinding="{binding name}"/> <gridviewcolumn width="150" header="age" displaymemberbinding="{binding age}"/> <gridviewcolumn width="150" header="location" displaymemberbinding="{binding location}"/> </gridview> </listview.view> </listview> <grid grid.column="1" verticalalignment="center" datacontext="{binding selectedclient,mode=twoway}"> <grid.columndefinitions> <columndefinition width="100"/> <columndefinition width="100"/> <columndefinition width="100"/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="auto"/> <rowdefinition height="auto"/> </grid.rowdefinitions> <textblock text="name: " grid.column="0" grid.row="0"/> <textblock text="age: " grid.column="0" grid.row="1"/> <textblock text="location: " grid.column="0" grid.row="2"/> <textbox grid.row="0" grid.column="1" text="{binding name}"/> <textbox grid.row="1" grid.column="1" text="{binding age}"/> <textbox grid.row="2" grid.column="1" text="{binding location}"/> </grid> </grid>

and in coresponding mainwindow.cs :

public partial class mainwindow : window, inotifypropertychanged { private client _selectedclient = new client(); public client selectedclient { { homecoming _selectedclient; } set { if (_selectedclient == value) return; _selectedclient = value; onpropertychanged(); } } private observablecollection<client> _clientcollection = new observablecollection<client>(); public observablecollection<client> clientcollection { { homecoming _clientcollection; } set { if (_clientcollection == value) return; _clientcollection = value; onpropertychanged(); } } public mainwindow() { initializecomponent(); clientcollection = new observablecollection<client>() { new client() { name = "james",age = 34, location = "paris" }, new client() { name = "joe",age = 34, location = "us" }, new client() { name = "ann",age = 34, location = "canada" }, }; } public event propertychangedeventhandler propertychanged; [notifypropertychangedinvocator] protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } } public class client { public string name { get; set; } public string location { get; set; } public int age { get; set; } }

and don't forget set datacontext mainwindow view, in case

datacontext="{binding relativesource={relativesource self}}"

c# wpf listview

No comments:

Post a Comment