binding - WPF ItemSource Not Working in XAML -


i writing chess ui in wpf.

i have set window datacontext in xaml:

<window.datacontext>     <local:mainviewmodel /> </window.datacontext> 

i've defined 'local' namespace namespace holds view model.

xmlns:local="clr-namespace:chessui" 

the view model has 1 property, collection of chess pieces:

public class mainviewmodel {     public observablecollection<chesspiece> chesspieces { get; set; }      public mainviewmodel()         :this(new observablecollection<chesspiece>())     {     }      public mainviewmodel(ienumerable<chesspiece> chesspieces)     {         this.chesspieces = new observablecollection<chesspiece>(chesspieces);     } } 

i've tried bind chesspieces chessboard (an itemscontrol) this:

<viewbox renderoptions.bitmapscalingmode="highquality">     <itemscontrol name="chessboard" itemssource="{binding chesspieces}">         [...]     </itemscontrol> </viewbox> 

but doesn't show pieces @ runtime. however, if uncomment line below works , see pieces on board.

public mainwindow() {     initializecomponent();     var viewmodel = new mainviewmodel(this.getstartingpositionchesspieces());     //this.chessboard.itemssource = viewmodel.chesspieces; } 

just clear:

with binding set in xaml:

no pieces!

with binding set in code:

with pieces!

anyone know i'm doing wrong xaml binding?

in code example,

public mainwindow() {     initializecomponent();     var viewmodel = new mainviewmodel(this.getstartingpositionchesspieces());     //this.chessboard.itemssource = viewmodel.chesspieces; } 

you creating viewmodel not using it. perhaps if assigned window's datacontext:

this.datacontext = viewmodel; 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -