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:
with binding set in code:
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
Post a Comment