6

嗨,我正在使用 MVVM 模式在 Windows 商店应用程序中工作,我在捕获中继命令中的 listview itemclick 值时遇到了一些问题。现在我得到了选定的项目值。但不知道如何获取 itemclickValue。在这里,我附上了我的代码。

XAML

 <ListView x:Name="lstItem" ItemTemplate="{StaticResource ItemTemplate}"  ItemsSource="{Binding ItemList}" Padding="130,0,0,0" SelectedItem="{Binding SelectedItem,Mode=TwoWay}">
            <Triggers:Interactions.Triggers>
                <Triggers:EventTrigger EventName="SelectionChanged">
                    <Triggers:InvokeCommandAction Command="{Binding SelectedItemCommand}" CommandParameter="{Binding SelectedItem,Mode=TwoWay}"/>
                </Triggers:EventTrigger>
            </Triggers:Interactions.Triggers>
        </ListView>

视图模型代码

private Item _selectedItem;
    public Item SelectedItem { get { return _selectedItem; } set { _selectedItem = value; NotifyPropertyChanged("SelectedTrends"); } }



private RelayCommand<Item> _selectedItemCommand;
    public RelayCommand<Item> SelectedItemCommand
    {
        get
        {
            return this._selectedItemCommand
                ?? (this._selectedItemCommand= new RelayCommand<Item>(item=>
                {
                    MessageDialog messagedialog = new MessageDialog(item.Name,"Test");
                    messagedialog.ShowAsync();
                }));
        }

    }
4

2 回答 2

8

这里有一点冗余:

选项 1:备用命令参数:

private Item _selectedItem;
public Item SelectedItem 
{ 
     get { return _selectedItem; } 
     set { _selectedItem = value; NotifyPropertyChanged("SelectedTrends"); } 
}



private RelayCommand _selectedItemCommand;
public RelayCommand SelectedItemCommand
{
    get
    {
        return this._selectedItemCommand
            ?? (this._selectedItemCommand= new RelayCommand(() =>
            {
                MessageDialog messagedialog = new MessageDialog(selectedItem.Name,"Test");
                messagedialog.ShowAsync();
            }));
    }

}

和 XAML:

<ListView x:Name="lstItem" ItemTemplate="{StaticResource ItemTemplate}"  ItemsSource="{Binding ItemList}" SelectedItem="{Binding SelectedItem,Mode=TwoWay}" Padding="130,0,0,0">
    <Triggers:Interactions.Triggers>
        <Triggers:EventTrigger EventName="SelectionChanged">
            <Triggers:InvokeCommandAction Command="{Binding SelectedItemCommand}" />
        </Triggers:EventTrigger>
     </Triggers:Interactions.Triggers>
 </ListView>

选项 2:备用 SelectedItem 绑定:

<ListView x:Name="lstItem" ItemTemplate="{StaticResource ItemTemplate}"  ItemsSource="{Binding ItemList}" Padding="130,0,0,0">
    <Triggers:Interactions.Triggers>
        <Triggers:EventTrigger EventName="SelectionChanged">
            <Triggers:InvokeCommandAction Command="{Binding SelectedItemCommand}" CommandParameter="{Binding SelectedItem, ElementName=lstItem}"/>
        </Triggers:EventTrigger>
     </Triggers:Interactions.Triggers>
 </ListView>

并从 ViewModel 中删除 SelectedItem 属性,除非您需要它用于其他用途。

编辑

如果要处理项目上的单击事件,则需要将行为移动到ItemTemplateDataTemplate 父控件,例如放置控件的网格。这使您可以处理项目上的单击事件。

于 2013-05-22T06:49:11.400 回答
0

为了解决这个问题,如果有 Null 引用,我评估了 setter 属性。然后它工作正常并且不再抛出事件选择其他元素。

<ListView Name="lstView" ItemsSource="{Binding Path=SimResults}" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding SelectedItemCommand}" CommandParameter="{Binding SelectedItem, ElementName=lstView}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="FileUniqueID" Width="Auto" DisplayMemberBinding="{Binding Path=FileUniqueID}" />
                <GridViewColumn Header="XML" Width="Auto" DisplayMemberBinding="{Binding Path=XML}" />
                <GridViewColumn Header="Request" Width="Auto" HeaderStringFormat="" DisplayMemberBinding="{Binding Path=RequestShort}" />
                <GridViewColumn Header="RequestDate" Width="Auto" DisplayMemberBinding="{Binding Path=RequestDate}" />
                <GridViewColumn Header="Response" Width="Auto" DisplayMemberBinding="{Binding Path=ResponseShort}" />
                <GridViewColumn Header="ResponseDate" Width="Auto" DisplayMemberBinding="{Binding Path=ResponseDate}" />
                <GridViewColumn Header="ResendCounter" Width="Auto" DisplayMemberBinding="{Binding Path=ResendCounter}" />
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>
于 2015-07-23T08:12:49.647 回答