简体   繁体   English

如何将自定义项目添加到列表框WPF C#

[英]how to add custom item to listbox wpf C#

I want to add custom items to list box in my program so I have this xaml code: 我想在程序中将自定义项添加到列表框中,所以我有以下xaml代码:

<ListBox Name="lb" Background="{x:Null}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Grid>
                        <Grid.ColumnDefinitions>                                                                                                                                                                                                                                                                                                                            
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding Num}" Grid.Column="0" Grid.RowSpan="2" Grid.Row="0"/>
                        <TextBlock Text="{Binding Pth}" Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="3"/>
                        <TextBlock Text="{Binding Name}" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="3"/>
                        <Button Content="{Binding EnterBtn}" Grid.Column="4" Grid.RowSpan="2"/>

                    </Grid>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

And i have this class: 我有这个课:

public class Item
{
    public string Pth { get; set; }
    public string Num { get; set; }
    public string Name { get; set; }
    public string EnterBtn { get; set; }
}

And in the mainwindow class i have this code: 在mainwindow类中,我有以下代码:

    string path_ = string.Format(@"C:\Users\{0}\Documents\folder path", Environment.UserName);
    List<Item> itms = new List<Item>();

    public MainWindow()
    {
        InitializeComponent();
        string[] arr = Directory.GetFiles(path_, ".emi");
        AddToList(arr);

    }
    private void AddToList(string[] a)
    {
        foreach(string s in a)
        {
            string fl;//first line
            string sl; //second line 
            using(StreamReader read = new StreamReader(s))
            {
                fl = read.ReadLine();
                sl = read.ReadLine();
            }
            itms.Add(new Item() { Num = (itms.Count + 1).ToString(), Name = sl, Pth = fl, EnterBtn = fl + sl });
        }
        lb.ItemsSource = itms;
    }

All i want is to read the text file that had (.emi) extension and and update that data to list box. 我想要的只是读取扩展名为(.emi)的文本文件,并将该数据更新到列表框中。

The problem is that nothing happens and the (.emi) files do exist. 问题是什么也没发生,并且(.emi)文件确实存在。

so is there any problem in my code 所以我的代码有什么问题吗

".emi" will only find files whose exact and complete name is ".emi" . ".emi"将仅查找确切和完整名称为".emi" You want "*.emi" -- the * means "this part could be any sequence of zero or more characters". 您需要"*.emi" - *表示“此部分可以是零个或多个字符的任何序列”。 That parameter is a "search pattern" , not a file extension. 该参数是“搜索模式” ,而不是文件扩展名。

string[] arr = Directory.GetFiles(path_, "*.emi");

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

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