This project has moved. For the latest updates, please go here.

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}" >
                            <StackPanel Margin="10" Orientation="Vertical" />
                            <Border x:Name="brdItem" Background="White" Margin="5" Tag="{Binding}">
                                            <Custom:EventTrigger EventName="MouseLeftButtonDown">
                                <TextBlock Foreground="Black" Text="{Binding Desc}"/>

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?

Jan 6, 2011 at 11:02 AM

It is also how I do it :)