我使用ItemsControl
來保存我的集合。 的ItemsPanel
是Canvas
,該ItemTemplate
是Border
>StackPanel
>TextBlocks
塊我要綁定在DataTemplate
一個命令捕獲在塊(我的收藏項目)的點擊在DataTemplate中的WPF命令綁定
代碼:
<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>
+1結合, -1爲'命令= 「{結合SomeCommand}」',DataTemplate中的DataContext設置產品(型號)不夠ViewModel – AymenDaoudi
不夠公平。編輯。 – Den