简体   繁体   English

C#WPF遍历ListView项

[英]C# WPF Iterate through ListView items

I've been going nuts trying to find a solution to what seems like a simple problem. 我一直在努力寻找解决方案,似乎是一个简单的问题。 I am trying to find a way to Iterate through my ListView and get values. 我正在尝试找到一种方法来遍历ListView并获取值。

I have a ListView with bindings which looks like this in xaml (simplified): 我有一个带有绑定的ListView,在xaml中看起来像这样(简化):

<ListView x:Name="MColumnsListXaml" HorizontalAlignment="Left" Height="Auto" Margin="0,42,0,0" VerticalAlignment="Top" Width="Auto">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="First Name" Width="Auto" DisplayMemberBinding="{Binding MColumnName}" />
            <GridViewColumn Header="Last Name" Width="Auto" DisplayMemberBinding="{Binding MColumnName2}" />
        </GridView>
    </ListView.View>
</ListView>

I declare bindings: 我声明绑定:

public class MandatoryColumns
{
    public string MColumnName { get; set; }
    public string MColumnName2 { get; set; }
}

Columns are populated by an event trigger which calls a method: 列由事件触发器填充,该事件触发器调用一个方法:

private void btnValidateColumns_Click(object sender, RoutedEventArgs e)
{
    MandatoryOptionalColumnsList();
}
public void MandatoryOptionalColumnsList()
{
    List<MandatoryColumns> MColumnsList = new List<MandatoryColumns>();
    MColumnsList.Add(new MandatoryColumns() { MColumnName = "John", MColumnName2 = "Smith" });
    MColumnsList.Add(new MandatoryColumns() { MColumnName = "Jason", MColumnName2 = "Bell" });
    MColumnsListXaml.ItemsSource = MColumnsList;
}

Now I need to get information that is in the ListView and that's where I'm stuck. 现在,我需要获取ListView中的信息,而这正是我遇到的问题。 My intention is to extract a column in a ListView and cross check against another list. 我的意图是在ListView中提取一列,然后对照另一个列表进行交叉检查。 To get the values, I've tried foreach loop as per below: 为了获取值,我尝试了如下的foreach循环:

public void test()
{
    foreach (var item in MColumnsListXaml.Items)
    {
        MessageBox.Show(MColumnsListXaml.ToString()); //MColumnsListXaml = {System.Windows.Controls.ListView Items.Count:2}
        MessageBox.Show(MColumnsListXaml.Items.ToString()); //MColumnsListXaml.Items = {System.Windows.Controls.ItemCollection}
        MessageBox.Show(item.ToString()); //item = {tutorialWpfApplication1.MandatoryColumns}
    }
}

In debugger I see item returns object {tutorialWpfApplication1.MandatoryColumns} and holds my needed information , but I can't find a way to access it. 在调试器中,我看到项返回对象{tutorialWpfApplication1.MandatoryColumns}并保存了我所需的信息 ,但是我找不到访问它的方法。 I tried to iterate item by making it dynamic , declare ListViewItem instead of `var and various solutions online, but everything always resulted in an exception error. 我试图通过使它dynamic来迭代item ,声明ListViewItem而不是`var和在线各种解决方案,但是一切总是导致异常错误。

Try this: 尝试这个:

foreach (var item in MColumnsListXaml.Items.OfType<MandatoryColumns>())
{
    MessageBox.Show(item.MColumnName);
    MessageBox.Show(item.MColumnName2);
}

After you set the value of ItemsSource you also can get the value. 设置ItemsSource的值后,您还可以获得该值。 So you can cast and use the list like 因此,您可以像这样投射和使用列表

List<MandatoryColumns> items = (List<MandatoryColumns>)MColumnsListXaml.ItemsSource;
foreach (MandatoryColumns item in items)
{
    //Do some work with the MandatoryColumns item
}

If you cast item to the type MandatoryColumn you should be able access the values. 如果将item MandatoryColumn转换为MandatoryColumn类型,则应该可以访问这些值。 As long as you know that every item in the list is of that type then you shouldn't get an exception when casting. 只要您知道列表中的每个项目都是该类型,那么在投射时就不会出现异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM