我想SelectedItems
在 ListView` 中获取值,但它不起作用!
选择一个项目时我没有问题,但我想在扩展模式下工作,以便显示任何选定的项目。
我的代码是:
List<Fnamelist> familylist = new List<Fnamelist>();
public class Fnamelist
{
public Fnamelist(string fname)
{
this.Fname = fname;
}
private string fname = string.Empty;
public string Fname
{
get { return fname; }
set { fname = value; }
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(((Fnamelist)listView1.SelectedItems).Fname.ToString());
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
familylist.Add(new Fnamelist("mike"));
familylist.Add(new Fnamelist("john"));
familylist.Add(new Fnamelist("melon"));
familylist.Add(new Fnamelist("bab"));
listView1.ItemsSource = familylist;
listView1.Items.Refresh();
}
xml:
<Button Content="show" Height="23" HorizontalAlignment="Left" Margin="331,79,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<ListView Height="129" HorizontalAlignment="Left" Margin="20,23,0,0" Name="listView1" VerticalAlignment="Top" Width="291">
<ListView.View>
<GridView>
<GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding Path=Fname}"/>
</GridView>
</ListView.View>
</ListView>
当我点击显示按钮时,它给出了错误:
Unable to cast object of type 'System.Windows.Controls.SelectedItemCollection' to type 'Fnamelist'.
有什么问题 ?