XAML 动态添加项目到WPF列表框

zour9fqk  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(120)

我试图构建一个应用程序,用户指向一个PDF文件的文件夹。如果没有,程序然后解析PDF文件,找出哪些包含电子邮件地址,哪些不包含。这就是我卡住的地方:
然后,我想将文件名添加到用于打印的列表框或用于电子邮件的列表框中。
我得到了所有其他位工作,选择文件夹和解析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"/>


但是我得到了错误:名称“ListofPrint”在命名空间“clr-namespace:test_app”中不存在。
ListofPrint在这里:

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


我一直在尝试获得的MSDN上的文档的窍门,并已阅读10个不同的类似的问题在这个网站上,但我想我的问题是,我不知道到底是什么我的问题是.首先,它是一个数据绑定的问题,但我基本上复制的示例从文档中玩,但这是什么给我的麻烦.
希望这里有人能向我解释数据绑定的基础知识,以及它如何与我的ObservableCollection相对应。

o2rvlv0m

o2rvlv0m1#

你需要创建一个你的collection类的示例,并将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>

相关问题