c# - WPF Data Binding confusion -


i've started taking course wpf , i'm bit confuse areas related data-binding. have no syntax issue, committed newbie errors , have couple of questions.

i've done simple screen 2 textboxes , when click button these 2 items added listbox.

reference within window tag of xaml people class

 xmlns:classes="clr-namespace:wpf_course.classes" 

added window resource

<window.resources>         <classes:people x:key="people"/> </window.resources> 

here's how i've declared listbox

<listbox datacontext="{binding source={staticresource people}}"                  itemssource="{binding persons}"                  x:name="personlistbox">              <listbox.itemtemplate>                 <datatemplate>                     <grid>                         <stackpanel>                             <textblock text="{binding fullname}"/>                         </stackpanel>                     </grid>                 </datatemplate>              </listbox.itemtemplate>                 </listbox> 

so, i've added datacontext listbox bind people resource , add itemsource looks on property within people .

this class

public class people : observablecollection<person>      {         public observablecollection<person> persons { { return persons; } set { persons = value; } }         private observablecollection<person> persons = new observablecollection<person>();          public people()         {             (int = 0; < 1; i++)             {                 // implicitly add 1 item messing around constructor                 persons.add(new person()                 {                     name = "dummy",                     lastname = "dummy",                     age = 15                 });             }         }     } 

based on i've done far have following questions :

1) what's difference between (they have same effect, there's more reasoning behind ? )

itemssource = "{binding persons}" 

and

itemssource = "{binding path = persons }" 

also leaving itemsource = "{binding}" instantiating people instance, logic being treated constructor of class ? i've messed around , appears so, not sure.

2) on peoples class i've implemented observablecollection<person> (where person class). doing static addition list constructor , had no properties defined within class ( observablecollection<person> type of properties ) needing ( implementatiton of interface) using property need it? , question :

if class's sole purpose load things within it's collection constructor ( , not outside class, needing sort of property ), best practice implement class observablecollection<myclass> or define properties of same type i've done ? (for accessing outside class)

i sorry weird questions because know sound silly, i'm looking validation because i've started working wpf recently.

thanks

edit 2 : thank answers, understood now. i've forgotten show how insert data in collection. ( added edit me remember if forget , others may stumble upon thread having similar confusion )

 observablecollection<person> ppl;     public mainwindow()         {             initializecomponent();             person = new person();             stackpanelperson.datacontext = person;              people = new people();             ppl = people.persons;             personlistbox.itemssource = ppl;         } 

initially doing this

 ppl.add(new person() { name = boxfirstname.text, lastname = boxlastname.text, age = int32.parse(boxage.text) }); 

then realized using data-binding on person class(inotifypropertychanged) properties changed :

 ppl.add(new person() { name = person.name, lastname = person.lastname, age = person.age}); 

thanks again guys replies !! have day !

question 1:

none, there no difference. {binding xyz} same {binding path=xyz}, it's shortcut. can used on first thing write in binding, example, cannot this:

{binding elementname=myelement, xyz} 

instead, this:

{binding elementname=myelement, path=xyz} 

or better:

{binding xyz, elementname=myelement} 

here's related question.

question 2:

what have done correct, collection of people should exposed property, why? because can bind it.

there no need static property in scenario.

i suggest researching mvvm design pattern. can find tutorial here


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -