我正在使用ItemsControl
来保存我的集合。
ItemsPanel
是一个Canvas
,ItemTemplate
是由一个Border
>StackPanel
>TextBlocks
组成的块。
我想要绑定一个命令到DataTemplate
,以捕获对块(集合项)的点击
代码:
<Grid Grid.Row="1" Grid.Column="1" >
<ItemsControl ItemsSource="{Binding Products}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<helpers:DragCanvas
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AllowDragging="True"
AllowDragOutOfView="False" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- The border and its content is what I see
on my canvas, I want to bind a command here (on click do something) -->
<Border BorderThickness="1" BorderBrush="Gold">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=Price}" />
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Command="{Binding SomeCommand}"
,扣1分;DataTemplate的DataContext设置为Product(Model),而不是ViewModel。 - AymenDaoudi