我有一个定义RelayCommand的ViewModel,在View i中定义了ListView和ListItemTemplate。我需要通过单击列表项来调用带有列表项值的命令。
视图中的ListView
<ListView x:Name="listView1" SelectionMode="Single" ItemsSource="{Binding SimilarArtists}" MinWidth="360">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="SelectionChanged">
<Core:InvokeCommandAction Command="{Binding ResolveSearchPageViewModel.SearchCommand, Converter={StaticResource SelectionChangedConverter}, Mode=OneWay, Source={StaticResource Locator}}" InputConverterParameter="{Binding ElementName=listView1}" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<Grid MinWidth="360">
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="48"/>
<ColumnDefinition MinWidth="360"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Width="48" Source="../Assets/singer.png"/>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Margin="10, 10, 0, 10" FontSize="25" Text="{Binding Name}" TextWrapping="Wrap" Grid.Row="0"/>
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
SelectionChangedConverter
public class SelectionChangedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var listView = parameter as ListView;
return listView.SelectedItem;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
RelayCommand
GoToSearchPageCommand = new RelayCommand<Artist>((item) =>
{
var it = item;
_pageNavigationService.ToLoadingPage(item.Name);
});
我需要传入SelectedItem的RelayCommand值并调用该命令。谢谢。最好的问候,德米特里
聚苯乙烯。我的英语不是很好,对不起。
答案 0 :(得分:0)
您是否考虑过将listview的SelectedItem属性绑定到您的视图模型的属性?
你不需要命令。只要您声明了绑定,就会为您映射SelectedItem。
只需在视图模型中添加一个属性,该属性反映了ItemsSource所基于的类型,并在XAML中绑定到该属性。
<ListView SelectedItem = "{Binding MySelectedItem}"