我写了一个listView
<ListView
SelectionChanged="MySelectionChanged"
ScrollViewer.CanContentScroll="False"
Background="Transparent"
x:Name="uiListView"
ItemTemplate="{StaticResource ItemTemplate}"
ItemsSource="{Binding Albums}"
Width="117"
Height="Auto"
BorderThickness="0"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden" />
并将其绑定到
public class ListImage
{
public ListImage(string cover, string title, string isvideo)
{
Cover = cover;
Title = title;
isVideo = isvideo;
}
public string Cover { get; set; }
public string Title { get; set; }
public string isVideo { get; set; }
}
我以这种方式添加项目:
uiListView.Items.Add(new ListImage(@"C:\tablet\3kings_full.jpg", @"Title", "false"));
uiListView.Items.Add(new ListImage(@"C:\tablet\3kings_full.jpg", @"Title", "true"));
uiListView.Items.Add(new ListImage(@"C:\tablet\3kings_full.jpg", @"Title", "false"));
uiListView.Items.Add(new ListImage(@"C:\tablet\3kings_full.jpg", @"Title", "true"));
uiListView.Items.Add(new ListImage(@"C:\tablet\3kings_full.jpg", @"Title", "false"));
uiListView.Items.Add(new ListImage(@"C:\tablet\3kings_full.jpg", @"Title", "true"));
uiListView.Items.Add(new ListImage(@"C:\tablet\3kings_full.jpg", @"Title", "false"));
uiListView.Items.Add(new ListImage(@"C:\tablet\3kings_full.jpg", @"Title", "true"));
uiListView.Items.Add(new ListImage(@"C:\tablet\3kings_full.jpg", @"Title", "false"));
uiListView.Items.Add(new ListImage(@"C:\tablet\3kings_full.jpg", @"Title", "true"));
一切都很好。我现在需要访问被点击元素的 ListViewItem 所以
private void MySelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(uiListView.Items[uiListView.SelectedIndex].GetType().ToString());
}
但这会返回 ListImage 类型而不是 ListViewItem。我怎样才能访问它?
此致