c# - Defining data source declaratively in XAML -


i have list controls small, static data sources. example:

<itemscontrol itemssource="{binding countries}" .../> 

and view model populates list:

this.countries.add(new country { code = "be", name = "belgium" }); this.countries.add(new country { code = "ca", name = "canada" }); // etc. 

is there alternative way define list contents in xaml instead? like:

<itemscontrol>     <itemscontrol.itemssource>         <somenamespace:list>             <mynamespace:country code="be" name="belgium" />             etc.         </somenamespace:list>     </itemscontrol.itemssource> </itemscontrol> 

i put lists in separate resource files , hope itemssource="{staticresource mylistofcountries}" after defining them resource.

i want lighten boilerplate code in vms. wonder if negatively affect performance objects created before view rendered, while otherwise load these later (on navigated to, on view load, ... vs contructor). thoughts welcome!

you can creating new collectiontype, , populating in xaml.

example,

collectiontype used in xaml:

using system.collections.objectmodel;  namespace wpfapplication4 {     public class countrycollection : observablecollection<country>     {     } } 

poco:

using system; using system.collections;  namespace wpfapplication4 {     public class country      {         public string name { get; set; }         public string code { get; set; }     } } 

xaml:

<window x:class="wpfapplication4.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:local="clr-namespace:wpfapplication4"         title="mainwindow" height="350" width="525">     <window.resources>         <local:countrycollection x:key="countrylist">             <local:country name="canada" code="ca"/>             <local:country name="united states" code="us"/>             <local:country name="belgium" code="be"/>         </local:countrycollection>     </window.resources>     <grid>         <itemscontrol itemssource="{staticresource countrylist}">             <itemscontrol.itemtemplate>                 <datatemplate>                     <stackpanel orientation="horizontal">                         <label content="{binding name}"/>                         <label content="{binding code}"/>                     </stackpanel>                 </datatemplate>             </itemscontrol.itemtemplate>         </itemscontrol>     </grid> </window> 

note, xaml provided similar to:

    var countrylist = new observablecollection<country>     {         new country {name = "canada", code = "ca"},         new country {name = "united states", code = "us"},         new country {name = "belgium", code = "be"}     }; 

edit (update using arraylist)

with collections namespace defined in xaml, can use

   xmlns:collections="clr-namespace:system.collections;assembly=mscorlib"  <window.resources>     <collections:arraylist x:key="countrylist">         <local:country name="canada" code="ca"/>         <local:country name="united states" code="us"/>         <local:country name="belgium" code="be"/>     </collections:arraylist> </window.resources> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -