私は行きItemsControl
ます。X、Y、ImageUri などのプロパティを持つ、ボタンを表すクラスが必要です。生成されたボタンを を介して公開しObservableCollection
、 にバインドしItemsSource
ますItemsControl
。ItemsPanelTemplate
次に、グリッドに変更します。
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<!--Here go rows and columns definitions-->
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
行と列の数が固定されている場合は、それらを XAML に直接追加できます。それ以外の場合は、コード ビハインドで実行時に生成します。配置のために追加ItemsContainerStyle
します:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Row" Value="{Binding Y}" />
<Setter Property="Grid.Column" Value="{Binding X}" />
</Style>
</ItemsControl.ItemContainerStyle>
ItemTemplate
また、表示ボタンを画像で覆う必要があります。その他のオプションは、Canvas
or UniformGrid
asを使用することですItemsPanelTemplate
(長所と短所の両方があります)。