ItemsControl Item MouseDown Event and ViewModel Command

Jan 6, 2011 at 10:43 AM

Hi, Yet another question, as i am quite new to mvvmlight. i have an ItemsControl and in its DataTemplate i have a border control at which i have placed EventToCommand Behavior with event MouseDown. But When i opened the page in blend and try to Bind that event to a command in ViewModel i saw only properties of my DTO, and that makes sense because ItemsControl Element have their DataContext limited to the DTO presented as a list in ItemsControl's ItemsSource Property. as it is a common scenario tell me how can i achieve this in mvvmlight? is it possible in mvvmlight?

<ScrollViewer Margin="8" >
                <ItemsControl x:Name="icTasks" ItemsSource="{Binding Tasks}" >
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Margin="10" Orientation="Vertical" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Border x:Name="brdItem" Background="White" Margin="5" Tag="{Binding}">
                                <Custom:Interaction.Triggers>
                                            <Custom:EventTrigger EventName="MouseLeftButtonDown">
                                                <GalaSoft_MvvmLight_Command:EventToCommand/>
                                            </Custom:EventTrigger>
                                </Custom:Interaction.Triggers> 
                               
                                <TextBlock Foreground="Black" Text="{Binding Desc}"/>
                            </Border>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>

Jan 6, 2011 at 10:53 AM

A little searching yeild the answer.

 Command="{Binding HomeViewModel.ItemTapCommand, Source={StaticResource Locator}}"

i can use Locator and then my ViewModel to access the ItemTapCommand. and it works fine.

I wanted to know is this the right way of doing this?

Coordinator
Jan 6, 2011 at 11:02 AM

It is also how I do it :)

Cheers,

Laurent