我一直在使用 MVVM 的 RelayCommand 成功地将操作绑定到 XAML,但是我的 ItemsControl 有一个小问题。
<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,对吗?)