动态添加项目到WPF列表框

时间:2017-05-27 10:21:41

标签: c# .net wpf xaml listbox

我正在尝试构建一个用户指向PDF文件夹的应用程序。 对于发票,程序然后解析PDF文件,找出哪些包含电子邮件地址,哪些不是。这就是我被困的地方:

然后我想将文件名添加到Listbox for print或Listbox for email。

我得到了所有其他功能,选择文件夹并解析PDF并将文件夹路径添加到文本框对象。

然后我运行一个函数:

private void listFiles(string selectedPath)
        {
            string[] fileEntries = Directory.GetFiles(selectedPath);
            foreach (string files in fileEntries)
            {

                try
                {
                    ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
                    using (PdfReader reader = new PdfReader(files))
                    {
                        string thePage = PdfTextExtractor.GetTextFromPage(reader, 1, its);
                        string[] theLines = thePage.Split('\n');
                        if (theLines[1].Contains("@"))
                        {
                           // System.Windows.MessageBox.Show("denne fil kan sendes som email til " + theLines[1], "Email!");

                        }
                        else
                        {
                            System.Windows.MessageBox.Show("denne fil skal Printes da " + theLines[1] + " ikke er en email", "PRINT!");
                        }
                    }
                }
                catch (Exception exc)
                {
                    System.Windows.MessageBox.Show("FEJL!", exc.Message);
                }



            }
        }

在这个功能中,我希望能够将文件添加到列表框中。

我的XAML看起来像这样:

<Grid.Resources>
        <local:ListofPrint x:Key="listofprint"/>
</Grid.Resources>

<ListBox x:Name="lbxPrint" ItemsSource="{StaticResource listofprint}" HorizontalAlignment="Left" Height="140" Margin="24.231,111.757,0,0" VerticalAlignment="Top" Width="230"/>

但是我得到了错误:名称&#34; ListofPrint&#34;命名空间中不存在&#34; clr-namespace:test_app&#34;。

ListofPrint在这里:

public class ListofPrint : ObservableCollection<PDFtoPrint>
    {
        public ListofPrint(string xfile)
        {
            Add(new PDFtoPrint(xfile));
        }
    }

我一直试图在MSDN上获取文档,并在此网站上阅读了10个不同的类似问题,但我想我的问题是我不确切地知道我的问题是什么。首先是数据绑定问题,但我基本上从文档中复制了样本,但这就是给我带来麻烦的。

希望有人在这里向我解释数据绑定的基础知识以及它与我的ObservableCollection的对应关系。

1 个答案:

答案 0 :(得分:1)

您需要创建集合类的实例并将ListBox绑定到它。 最简单的事情是将DataContext设置为this。我写了一个例子:

窗口:

public class MyWindow : Window
{
    // must be a property! This is your instance...
    public YourCollection MyObjects {get; } = new YourCollection();

    public MyWindow()
    {
        // set datacontext to the window's instance.
        this.DataContext = this;
        InitializeComponent();
    }

    public void Button_Click(object sender, EventArgs e)
    {
        // add an object to your collection (instead of directly to the listbox)
        MyObjects.AddTitle("Hi There");
    }
}

您的notifyObject集合:

public class YourCollection : ObservableCollection<MyObject>
{
    // some wrapper functions for example:
    public void Add(string title)
    {  
       this.Add(new MyObject { Title = title });
    }
}

项目类:

// by implementing the INotifyPropertyChanged, changes to properties
// will update the listbox on-the-fly
public class MyObject : INotifyPropertyChanged
{
     private string _title;

     // a property.
     public string Title
     {
         get { return _title;}
         set
         {
             if(_title!= value)
             {
                 _title = value;
                 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs( nameof(Title)));
             }
         }
     }
     public event PropertyChangedEventHandler PropertyChanged;
}

的Xaml:

<ListBox ItemsSource="{Binding MyObjects}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>