0

I am implementing a listbox using MVVM approach and I'm having an issue where my double click command is not being triggered. When I launch my application I see my ListBox populated via binding just fine. But when I double click on an item nothing happens. Is there something I'm missing? Many thanks in advance.

Here is how I have my UserControl (xaml) set up

    <ListBox 
        x:Name="Files" 
        ItemsSource="{Binding FileCollection, Mode=TwoWay}"
        SelectedItem="{Binding Filename, Mode=TwoWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}">                                    
                    <TextBlock.InputBindings>
                        <MouseBinding  
                         Gesture="LeftDoubleClick" 
                         Command="{Binding EditFileCommand}"/>
                    </TextBlock.InputBindings>
               </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

This is how I am setting up my command object in my View Model:

        //..using system.windows.input for ICommand
        private ICommand editFileCommand = null;
        public ICommand EditFileCommand
        {
            get
            {
                //RelayCommand comes from GalaSoft.MvvmLight.Command
                return editFileCommand ?? new RelayCommand(EditFile); 
            }
        }

        private void EditFile()
        {
            MessageBox.Show("Double Click!");
        }
user2529011
  • 705
  • 3
  • 11
  • 21
  • Did you already check out [those](https://stackoverflow.com/questions/6938752/wpf-how-do-i-handle-a-click-on-a-listbox-item) SO [questions](https://stackoverflow.com/questions/10207888/wpf-listview-detect-when-selected-item-is-clicked)? – dymanoid Aug 06 '19 at 14:41
  • @dymanoid Thank you for the prompt reply. I checked them out but they don't seem to be using MVVM. – user2529011 Aug 06 '19 at 14:44
  • @user2529011: Where is the command defined? – mm8 Aug 06 '19 at 14:55

2 Answers2

1

This is almost similar to RelayCommand, you can use it like this:

Declare in your ViewModel:

public RelayCommand EditFileCommand { get; set; }

Then, you need to initialize it:

EditFileCommand = new RelayCommand(EditFile);

The XAML remains equal:

<ListBox 
        x:Name="Files" 
        ItemsSource="{Binding FileCollection, Mode=TwoWay}"
        SelectedItem="{Binding Filename, Mode=TwoWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}">                                    
                    <TextBlock.InputBindings>
                        <MouseBinding  
                         Gesture="LeftDoubleClick" 
                         Command="{Binding EditFileCommand}"/>
                    </TextBlock.InputBindings>
               </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
  • 1
    thaaaaaaaaank youuuuuu! I did some more digging on RelayCommand and not only does it inherit from ICommand but just making my object RelayCommand I didnt have to do all that extra stuff to my EditFileCommand. Just one line to initialize and done. Thank you again. Rock on! – user2529011 Aug 06 '19 at 15:28
1

If the command property is defined in the File class, it should work provided that you actually click on the TextBlock. You could make the TextBlock stretch across the ListBoxItem container by using an ItemContainerStyle:

<ListBox x:Name="Files" ...>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            ...
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

If the command is defined in the view model where the FileCollection property is defined, you should use a RelativeSource:

<TextBlock.InputBindings>
    <MouseBinding  Gesture="LeftDoubleClick" 
                   Command="{Binding DataContext.EditFileCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"/>
</TextBlock.InputBindings>
mm8
  • 163,881
  • 10
  • 57
  • 88