ボタンを横並びに動的に追加したい
少し嵌ったのでメモ
DataContextは設定されてる前提
<ListBox ItemsSource="{Binding Items}"> <!--データの表示形式を決める--> <ListBox.ItemTemplate> <DataTemplate> <Button Content="{Binding Tell}"/> </DataTemplate> </ListBox.ItemTemplate> <ListBox.ItemsPanel> <ItemsPanelTemplate><!--使用するパネルを決める--> <StackPanel Orientation="Horizontal"> </StackPanel> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox>
イメージとしては
方法 : 水平方向の ListBox を作成する
の中身をボタンにしたいといった所でしょうか。
<ListBox.ItemsPanel>
はあくまでパネルの形を決定するのですね。ListBox内の表示形式についてはとやかく言わない。
容器だけ用意するからあと勝手にやってよ、というイメージでしょうか。
<ListBox.ItemTemplate>
ListBox内のデータをどのように表示するか決めます。itemのTemplateだからItemTemplate。なるほどね。