我一直在使用MVVM的RelayCommand成功地將操作綁定到XAML,但是我的ItemsControl有一個小問題。來自ItemsControl的RelayCommand發件人項目
<ItemsControl ItemsSource="{Binding Devices}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Width="100" Margin="4" >
<Button Command="{Binding Path=SelectDeviceCommand}" >
<Grid>
<Image Source="img_small.png"></Image>
<Image Source="{Binding Path=Logo}" />
</Grid>
</Button>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
在我的視圖模型:
public RelayCommand SelectDeviceCommand { get; set; }
private ObservableCollection<Device> Devices;
Devices = CreateListOfDevices();
private void InitializeCommands()
{
SelectDeviceCommand = new RelayCommand((s) => MessageBox.Show(s.ToString()));
}
如何定義我的SelectDeviceCommand在我看來模式,以獲得綁定到該項目的對象?
我SelectDeviceCommand甚至不被稱爲...(但我猜是因爲我需要讓我的設備的迷你視圖模型,並在其中執行SelectDeviceCommand,是正確的?)