c# - Bind Collection to StackPanel -
I want to get a collection of objects and tie it in the stackpack so that basically 4 elements in the collection inside the stack Ho. The panel that says should produce 4 buttons.
I tried to do it ... but I think that its correct approach is not anyway. I had datatompled this kind of idea in the past. Please correct me if I am wrong.
I have a fake model
Public class Mockmodd {public supervision collection & lt; Mock node & gt; Nodes; Public Mock Model () {nodes = New Observation Collection & lt; MockNode & gt; (); }} Public Square Mock Node {Public MockNode () {} Private string itemname; Public string itemName {receive {this.itemname return; } Set {this.itemname = value; }}}
In the code I set DataContext like this ...
// Init Model Mock model myModel = new MockModel (); For (int i = 0; i
and xaml
& lt; StackPanel X: Name = "tStack" & gt; & Lt; ItemsControl ItemSource = "{Binding Nodes}" & gt; & Lt; ItemsControl.Template & gt; & Lt; ControlTemplate & gt; & Lt; Button content = "{binding object name}" /> & Lt; / ControlTemplate & gt; & Lt; /ItemsControl.Template> & Lt; / ItemsControl> & Lt; / StackPanel & gt;
It's a bind but instead of 4 buttons I get only one button ....
Thoughts?
OK I have understood it ... using a ItemsControl
The problem is resolved by ...
& lt; Itemscontrol x: name = "tStack" grid. Column = "0" & gt; & Lt; ItemsControl.ItemsPanel & gt; & Lt; ItemsPanelTemplate & gt; & Lt; StackPanel Orientation = "Horizontal" /> & Lt; / ItemsPanelTemplate & gt; & Lt; /ItemsControl.ItemsPanel> & Lt; ItemsControl.ItemTemplate & gt; & Lt; DataTemplate & gt; & Lt; Button content = "{binding object name}" /> & Lt; / DataTemplate & gt; & Lt; /ItemsControl.ItemTemplate> & Lt; / ItemsControl>
Comments
Post a Comment