简体   繁体   English

Listview 2列WPF

[英]Listview 2 columns WPF

Another newbie question. 另一个新手问题。 I would like to open a text file and bind to a listview(WPF) with 2 columns, ID and Details. 我想打开一个文本文件并将其绑定到具有2列ID和详细信息的listview(WPF)。 Here is a copy of the txt file. 这是txt文件的副本。 So ID will contain host and os name and so on and Details will contain the info to the right. 因此,ID将包含主机名和操作系统名称,以此类推,而Details将包含右侧的信息。 This list is much larger, but you get the point. 这个列表更大,但是您明白了。 Yes I am new to c# and WPF. 是的,我是C#和WPF的新手。 Your help is appreciated.. 感谢您的帮助。

Host Name:                 MD1HXQTC
OS Name:                   Microsoft Windows 7 Enterprise
OS Version:                6.1.7601 Service Pack 1 Build 7601
BIOS Version:              Hewlett-Packard M70 Ver. 01.08, 3/16/2015

The ListView is a descendant of the ListBox that allows to define custom views rather than a straight list. ListView是ListBox的后代,允许定义自定义视图而不是直接列表。

The easiest way to get a list with columns is to have a GridView as the view of your ListView. 获取带有列的列表的最简单方法是将GridView作为ListView的视图。

So if you have a class like this: 因此,如果您有这样的课程:

public class SomeClass
{
    public string FirstProperty { get; set; }
    public string SecondProperty { get; set; }
}

//Actually you will also need your setters to raise the PropertyChanged event but it's not in the scope of the question.

You can declare your ListView as follows: (notice the DisplayMemberBindings have the names of my properties) 您可以按以下方式声明ListView:(注意DisplayMemberBindings具有我的属性的名称)

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="First Property"
                            DisplayMemberBinding="{Binding FirstProperty}"/>
            <GridViewColumn Header="Second Property"
                            DisplayMemberBinding="{Binding SecondProperty}"/>
        </GridView>
    </ListView.View>
</ListView>

Then you will need to make your collection of SomeClass to be the ItemsSource of the ListView. 然后,您需要使SomeClass的集合成为ListView的ItemsSource。

Created My Class 建立我的课程

public class Gambit
{
    public string GetID { get; set; }
    public string GetDetails { get; set; }
}

Added Observable Collection 添加了可观察的集合

private ObservableCollection<Gambit> gambitCollection = new ObservableCollection<Gambit>();

Wrote method to import txt file and bind objects 写入txt文件并绑定对象的写方法

Microsoft.Win32.OpenFileDialog openfiledialog = new Microsoft.Win32.OpenFileDialog();
        openfiledialog.FileName = ""; // Default file name
        openfiledialog.DefaultExt = ".txt"; // Default file extension
        openfiledialog.Filter = "txt files (.txt)|*.txt"; // Filter files by extension

        // Show open file dialog box



        bool? result = openfiledialog.ShowDialog();

        // Process open file dialog box results
        if (result == true)
        {

            var fileName = openfiledialog.FileName;

            StreamReader file = new StreamReader(fileName);

            string line = "";

            while ((line = file.ReadLine()) != null)
            {
                var splitLine = line.Split(':');


                if (splitLine.Count() > 1)
                {
                    gambitCollection.Add(new Gambit() { GetID = splitLine[0].Trim(), GetDetails = splitLine[1].Trim() });
                }
            }

        }

Binded Itemsource to Collection 绑定项目源到集合

ListViewRPMInfo.ItemsSource = gambitCollection;

Thank you to Babgev for getting me started and all others who pointed me in directions. 感谢Babgev让我入门,以及所有其他向我指出方向的人。

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

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