gpt4 book ai didi

windows-phone-7 - 在 ItemsControl Tap 事件上绑定(bind) CommandParameter

转载 作者:行者123 更新时间:2023-12-04 07:10:36 24 4
gpt4 key购买 nike

我正在使用 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 ....

这是我的 View 模型:
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 执行此操作的任何想法?还是性能差异在 Mango 中两者之间没有太大区别?

最佳答案

您的 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>
...

关于windows-phone-7 - 在 ItemsControl Tap 事件上绑定(bind) CommandParameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10841642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com