首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF中带MVVM的ListItem上的ListItem

WPF中带MVVM的ListItem上的ListItem
EN

Stack Overflow用户
提问于 2015-09-03 09:42:24
回答 1查看 516关注 0票数 3

当我双击任何列表项时,我想显示对话框。但是程序的流永远不会出现在ShowTextCommand属性中。我得到了名字的列表(这很好),但我无法得到对话框。这是我的XAML:

代码语言:javascript
复制
<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>

这是我的指挥课:

代码语言:javascript
复制
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
}

和视图模型

代码语言:javascript
复制
 private ICommand command;
    public ICommand ShowTextCommand
    {
        get
        {
            if (command == null)
                command = new EnterTextCommand(this);
            return command;
        }
 internal void EnterText()
    {
        MessageBox.Show("Event Success");
    }

有人能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-03 10:00:34

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

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32372153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档