ar.gorgin
ar.gorgin

Reputation: 5002

How to bind MouseDoubleClick event in mvvm of ListView

I have a ListView with multi Columns.

I want to run a command when double click on a row. I use EventTrigger for this. But when i double click on every where of ListView run command. But I want to run this command when double click on ListViewItem.

<i:Interaction.Triggers>
  <i:EventTrigger EventName="MouseDoubleClick">
    <cmd:EventToCommand Command="{Binding ShowLetterCommand}" CommandParameter="{Binding SelectedItem, ElementName=DashboardListView}" />
  </i:EventTrigger>
</i:Interaction.Triggers>

Upvotes: 2

Views: 4342

Answers (2)

StepUp
StepUp

Reputation: 38094

You should call CallMethodAction from System.Windows.Interactions library and point at your method name at MethdoName like that:

I've made an example and it perfectly works:). You need two libraries to handle MouseDoubleClick event:

  • System.Windows.Interactivity(Address at my computer is C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries\)
  • Microsoft.Expression.Interactions(Address at my computer is C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries\)

XAML:

<UserControl
 ...the code omitted for the brevity...
  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
  xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
...the code omitted for the brevity...
>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="5*"/>
    </Grid.ColumnDefinitions>             
   <ListView ItemsSource="{Binding Persons}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <ei:CallMethodAction MethodName="DoubleClickMethod" TargetObject="{Binding}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding IdPerson}" Margin="0,0,5,0"/>
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>                
        </ListView.ItemTemplate>
    </ListView>
</Grid>
</UserControl>

ViewModel:

public void DoubleClickMethod()
{
   MessageBox.Show("It is a Double Click");
   /*        if(parameter!=null)
       YourClass aClass=(YourClass)parameter; 
   */
}

private ObservableCollection<Person> persons;
public ObservableCollection<Person> Persons
{
   get { return persons; }
   set
   {
      persons = value;
      OnPropertyChanged("Persons");
   }
}

public MainWindowViewModel()
{
   LoadPersons();
}

private void LoadPersons()
{
   persons = new ObservableCollection<Person>();
   for (int i = 0; i < 20; i++)
   {
      Persons.Add(new Person() { IdPerson = i, Name = "Charlie " + i.ToString()});
   }

}

Model:

public class Person
{
    public int IdPerson { get; set; }
    public string Name { get; set; }
}

The link to download a test project.

Upvotes: 3

Mohan Tangella
Mohan Tangella

Reputation: 19

you can use Inputbindings to achieve this.

<ListView ItemsSource="{Binding SampleListData,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedTree,Mode=TwoWay}" Name="dgSample">
        <ListView.InputBindings>
            <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding ListDataEdited}"/>
        </ListView.InputBindings>
</ListView>

bind selected row to 'SelectedItem' .

View Model :

 #region Commands
    public RelayCommand ListDataEdited
    {
        get
        {
            return new RelayCommand(listDataEdited);
        }
    }
    #endregion

    #region Methods
    private void listDataEdited()
    {
        if (SelectedTree != null)
        {
            //code here
        }
    }
    #endregion

Upvotes: 0

Related Questions