Oreshkovski
Oreshkovski

Reputation: 69

MouseDoubleClick on ListItem with MVVM in WPF

I want to show dialog when i double click any of the list items. But the flow of the program never go in the ShowTextCommand property. I get the list of the names (that works fine) but I can't get the dialog. This is my XAML:

<ListView  ItemsSource="{Binding List}"  >
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}">
                    <TextBlock.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick"  Command="{Binding ShowTextCommand, UpdateSourceTrigger=PropertyChanged}"></MouseBinding>
                    </TextBlock.InputBindings>
                </TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

this is my Command class:

public class EnterTextCommand : ICommand
{
    public EnterTextCommand(TekstoviViewModel vm)
    {
        ViewModel = vm;
    }
    private TekstoviViewModel ViewModel;
    #region ICommand interface
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
    public bool CanExecute(object parameter)
    {
        return true;
       // return ViewModel.CanExecute;
    }


    public void Execute(object parameter)
    {
        ViewModel.EnterText();
    }
    #endregion
}

and the view model

 private ICommand command;
    public ICommand ShowTextCommand
    {
        get
        {
            if (command == null)
                command = new EnterTextCommand(this);
            return command;
        }
 internal void EnterText()
    {
        MessageBox.Show("Event Success");
    }

Can someone help ?

Upvotes: 3

Views: 528

Answers (1)

SamTh3D3v
SamTh3D3v

Reputation: 9944

Your DataTemplate can't find the command, specify the full path to it using ElementName Binding

<ListView  ItemsSource="{Binding List}"  x:Name="MainList">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}">
                    <TextBlock.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick"  Command="{Binding DataContext.ShowTextCommand, UpdateSourceTrigger=PropertyChanged,ElementName=MainList}"></MouseBinding>
                    </TextBlock.InputBindings>
                </TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Upvotes: 1

Related Questions