有人可以帮助我使用网格列表视图。我想当你点击一个调用函数的元素时,我得到当前项目。我已经有以下XAML代码:
<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>
在cs文件中,我有以下功能:
private Item selectedItem;
private RelayCommand selectedItemCommand;
this.selectedItemCommand = new RelayCommand(this.SelectedItems);
public Item SelectedItem
{
get { return selectedItem; }
set { selectedItem = value; OnPropertyChanged("SelectedTrends"); }
}
public RelayCommand SelectedItemCommand
{
get
{
return this.selectedItemCommand;
}
}
private void SelectedItems(object obj)
{
this.requestXml = this.selectedItem.DisplayName;
}
当我选择 selectedItems 元素时,我可以获得该项目。
您好,我测试了上面的代码,它工作正常。但我有问题。我有不同的选项卡,当我单击左键单击其他选项卡时,它始终运行SelectionChanged事件,我得到一个Null引用异常。
答案 0 :(得分:1)
请确保在ctor中的InitializeComponent()之前初始化selectedItemCommand,例如:
public MainWindow()
{
this.selectedItemCommand = new RelayCommand(this.SelectedItems);
InitializeComponent();
}
答案 1 :(得分:1)
一种解决方案是从listView创建“SelectedItem”的属性。
所以,在你的VM中:
private YourType _selectedResult;
public YourType SelectedResult
{
get { return _selectedResult; }
set { _selectedResult= value; OnPropertyChanged("SelectedResult"); }
}
并在ListView上绑定此属性:
<ListView Name="lstView" ItemsSource="{Binding Path=SimResults}" SelectedItem="{Binding SelectedResult}">