user2529011
user2529011

Reputation: 743

DoubleClick on my ListBox item is not being fired

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!");
        }

Upvotes: 0

Views: 341

Answers (2)

mm8
mm8

Reputation: 169390

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>

Upvotes: 1

Catarina Ferreira
Catarina Ferreira

Reputation: 1854

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>

Upvotes: 1

Related Questions