RelayCommand sender from ItemsControl item

Posted by Padu Merloti on Stack Overflow See other posts from Stack Overflow or by Padu Merloti
Published on 2010-05-11T01:46:05Z Indexed on 2010/05/11 1:54 UTC
Read the original article Hit count: 487

Filed under:
|
|
|

I've been using MVVM's RelayCommand with success to bind actions to XAML, but I'm having a small problem with my 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>

In my view model:

    public RelayCommand SelectDeviceCommand { get; set; }
    private ObservableCollection<Device> Devices;

    Devices = CreateListOfDevices();

    private void InitializeCommands()
    {
        SelectDeviceCommand = new RelayCommand((s) => MessageBox.Show(s.ToString()));
    }

How do I define my SelectDeviceCommand in my view model in order to receive object that is bound to that item?

My SelectDeviceCommand is not even being called... (but that I guess is because I need to make my Device a mini-viewmodel and implement the SelectDeviceCommand in it, is that correct?)

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf