加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-7 – 在ItemsControl Tap事件上绑定CommandParame

发布时间:2020-12-14 05:36:24 所属栏目:Windows 来源:网络整理
导读:我正在使用ItemsControl,我想确定在Tap命令中选择了哪个项目.我的xaml在这里定义: ItemsControl ItemsSource="{Binding AllMyItems}" i:Interaction.Triggers i:EventTrigger EventName="Tap" cmd:EventToCommand Command="{Binding ItemSelectedCommand}"
我正在使用ItemsControl,我想确定在Tap命令中选择了哪个项目.我的xaml在这里定义:

<ItemsControl ItemsSource="{Binding AllMyItems}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
              <cmd:EventToCommand Command="{Binding ItemSelectedCommand}" CommandParameter="{Binding}"/>
         </i:EventTrigger>
    </i:Interaction.Triggers>
    .... item template ....

这是我的视图模型:

public RelayCommand<MyItem> ItemSelectedCommand { get; private set; }
public MainViewModel()
    {
        ItemSelectedCommand = new RelayCommand<MyItem>(ItemSelected);
    }

private void ItemSelected(MyItem myItem)
    {
        throw new NotImplementedException();
    }

命令的事件有效,但是当我到达ItemSelected方法时,myItem是Null,或者我得到一个异常(我取决于我在xaml中定义CommandParameter的方式).

如果我使用ListBox并设置CommandParameter =“{Binding SelectedItem,ElementName = MyItemsList”},我可以这样做

有关如何使用ItemsControl执行此操作的任何想法?或者说芒果差异在芒果中两者之间的差异不大吗?

解决方法

您的Tap事件发生在ItemsControl上,您应该将EventToCommand放在ItemTemplate中,一些XAML为您清除事物

<ItemsControl ItemsSource="{Binding AllMyItems}">
<ItemsControl.ItemTemplate>
<...>
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Tap">
          <cmd:EventToCommand Command="{Binding ItemSelectedCommand}" CommandParameter="{Binding}"/>
     </i:EventTrigger>
</i:Interaction.Triggers>
</...>                                    
</ItemsControl.ItemTemplate>
...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读