从多个项目中获取项目选择ListView WPF c#

时间:2017-07-06 20:07:14

标签: c# wpf listview

我需要帮助才能在列表视图中获取多个项目的选定值。

这是我的XAML代码:

<ListView x:Name="lb_employees" HorizontalAlignment="Left" Height="140" Margin="141,16,0,0" VerticalAlignment="Top" Width="255" Loaded="lb_employees_Loaded" SelectionChanged="lb_employees_SelectionChanged">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" />
                <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" />
                <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=empid}" Width="0" />
            </GridView>
        </ListView.View>
    </ListView>

以下是我填充ListView的方法:

dbml.hrdbDataContext listpeople = new dbml.hrdbDataContext();
        var q = from p in listpeople.EmpIDs
                join pi in listpeople.EmpBasicFiles on p.EmpID1 equals pi.EBF_EmpID
                where pi.EBF_Active == 1
                orderby p.LastName
                select new classes.Employeeselected() { FirstName = p.FirstName, LastName = p.LastName, empid = p.EmpID1 };

        foreach (var item in q)
        {
            this.lb_employees.Items.Add(item);
        }

我怎样才能得到let的话,选择后是什么?

我已经尝试过这段代码,但它根本不起作用。

        private void lb_employees_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        foreach (classes.Employeeselected item in lb_employees.SelectedItems)
        {
            System.Windows.MessageBox.Show(item.empid.ToString());
        }

    }

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:-1)

太奇怪了。当我之前编译它时,它一直给我一个null异常错误。但是现在,它似乎工作得很好!

重新启动后,它似乎正常工作。也许这是Insider Build的一个问题?