【问题标题】:MouseDoubleClick on ListItem with MVVM in WPF在 WPF 中使用 MVVM 对 ListItem 进行 MouseDoubleClick
【发布时间】:2015-11-29 01:59:48
【问题描述】:

我想在双击任何列表项时显示对话框。但是程序的流程永远不会进入 ShowTextCommand 属性。我得到了名称列表(工作正常),但我无法得到对话框。这是我的 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>

这是我的命令类:

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
}

和视图模型

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

有人可以帮忙吗?

【问题讨论】:

    标签: wpf xaml mvvm binding command


    【解决方案1】:

    您的DataTemplate 找不到command,使用ElementName 绑定指定它的完整路径

    <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>
    

    【讨论】:

      猜你喜欢
      • 2012-06-04
      • 2023-03-21
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      相关资源
      最近更新 更多