[英]multiple and single row selection listview with a gridview wpf
I have a question regarding wpf and ListView
s. 我对wpf和ListView
有疑问。 I have this program code in my Windows.xaml 我的Windows.xaml中有此程序代码
<Grid>
<ListView Margin="10" Name="lvUsers">
<ListView.View>
<GridView>
<GridViewColumn Header="Names" Width="140" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Telefon" Width="60" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Adress" Width="180" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView.View>
</ListView>
And in my Windows.xaml.cs i have this code 在我的Windows.xaml.cs中,我有以下代码
public partial class ListViewGridViewSample : Window
{
public ListViewGridViewSample()
{
InitializeComponent();
List<User> items = new List<User>();
items.Add(new User() { Name = "molo", Telefon= 0909090, Adress= "Street" });
items.Add(new User() { Name = "moloi", Telefon="99999", Adress= "street2" });
}
}
And my question is, when I execute this program code I get a window with a ListView
. 我的问题是,当我执行此程序代码时,我得到一个带有ListView
的窗口。 And now I want to make a selection at a single or multiple rows. 现在,我想在单行或多行中进行选择。 Maybe I want to select row number two this would be the User moloi and after selecting this row I want to pass on to a message box for example. 也许我想选择第二行,这将是用户moloi ,选择此行后,我想传递给消息框。 How can I handle that? 我该如何处理?
Can someone give me an easy example pls 有人可以给我一个简单的例子吗
thank you all 谢谢你们
ListView has a property called SelectionMode="Extended"
. ListView具有一个名为SelectionMode="Extended"
的属性。 That will allow you to set the desired selection mode. 这样您就可以设置所需的选择模式。 To make the selection behavior, you can use SelectionChanged
event that is also available on the ListView
. 要进行选择行为,可以使用ListView
上也提供的SelectionChanged
事件。 these two things can help you achieve everything you stated above. 这两件事可以帮助您实现上述所有内容。
<ListView Name="lvUsers"
ItemsSource="{Binding}"
Margin="10"
SelectionMode="Extended"
SelectionChanged="lvUsers_SelectionChanged">
<ListView.View>
And the code behind for the event: 以及该事件的代码:
private void lvUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//e.AddedItems - these are the items that have currently been selected
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.