WPF将DataTable绑定到重复的用户控件 [英] WPF Bind DataTable to repeated user controls

查看:201
本文介绍了WPF将DataTable绑定到重复的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在教自己的WPF,我遇到一个小问题,我找不到答案。

I'm in the process of teaching myself WPF, and I have run into a small issue that I can't find the answer to.

我的测试应用程序允许将图像文件放入StackPanel中。一旦图像被删除,新的用户控件将添加到堆栈中,并显示一些关于该文件的元数据。所有的都正常工作,我可以遍历子控件来检索值。

My test app allows image files to be dropped into a StackPanel. Once an image is dropped, a new user control is added to the stack, and displays some meta-data about the file. All is working correctly, and I can iterate through the child controls to retrieve the values.

我想要做的是允许用户持久化数据到一个文件,所以他们可以暂停处理数据。我这样做的明显方法是将数据存储在DataTable中,并将其序列化/反序列化为xml。但是,我不知道如何从DataTable或DataSet对象驱动用户控件的集合,实际上我甚至不知道这是WPF应用程序中的正确方法。我非常愿意承认我在这里的无知,如果有的话可以采取更好的建议。

What I'd prefer to be able to do is allow the user to persist this data to a file, so they can suspend working on the data. The obvious way for me to do this is to store the data in a DataTable and serialise/deserialise it to xml. However, I don't know how to drive the collection of user controls from a DataTable or DataSet object - in fact, I don't even know if this is the right way to go about it in a WPF app. I am more than willing to admit my ignorance here and take better suggestions if there are any.

应用程序逻辑摘要。

1)文件被丢弃(从Win explorer)到StackPanel

2)文件触发创建一个新的用户控件,它被添加到StackPanel

3 )数据填充在用户控件中

4)处理数据涉及遍历控件集合。

1) File is dropped (from Win explorer) onto a StackPanel
2) File triggers creation of a new user control, which is added to the StackPanel
3) Data is populated in the user control
4) Processing data involves iterating through the control collection.

我想要的

1)文件被丢弃(从Win explorer)到StackPanel

2)文件数据被插入到一些持久性对象(数据表?)中

3)更新的数据表驱动生成要添加到显示集合中的用户控件。

4)保存/加载功能会使数据在以后再次使用。

1) File is dropped (from Win explorer) onto a StackPanel
2) File data is inserted into some persistable object (data table?)
3) updated data table drives the generation of the user control to be added to the displayed collection.
4) save / load functionality persists the data for re-use later.

提前感谢

推荐答案

你在正确的轨道上使用第二种方法,您需要查看的是ItemsControl - 这是可以添加项目的东西。它是ListBox等的基础,您可以模板它按照需要工作。那么当一个项目被添加到底层数据结构中时,DataTemplate可以处理显示哪些控件和数据绑定到这些控件。网上有很多例子,请 WPF博士

You're on the right track with the second approach, what you need to look at is the ItemsControl - that's a thing which can have items added to it. It's the base for ListBox etc, and you can template it to work as you require. Then there's the DataTemplate which handles which controls are displayed and data binding to those controls when an item is added to the underlying data structure. There are quite a few examples around on the net, try Dr WPF.

为了使一切正常工作,底层数据结构必须支持更改通知。随着所有事情都会自动发生,一旦安装了Xaml,您就可以发现自己处于一种奇怪的状况。您已经将数据添加到数据结构中,从而导致控件和数据显示在您的ItemsControl中。如何链接数据项及其视觉控件。答案是使用一些内置的静态方法 ItemFromContainer 从图形到底层数据项的链接,用于处理点击事件,以及 ContainerFromItem ,反之亦然。

In order to make everything work the underlying data structure must support change notification. As everything happens automagically, once the Xaml is setup, you can find yourself in an odd situation. You've added data to a data structure, which in turn has caused controls and data to appear in your ItemsControl. How do you link the data items and their visual controls. The answer is to use some built in static methods ItemFromContainer which links from the graphic to your underlying data item, useful to handle click events, and ContainerFromItem which does the reverse.

这篇关于WPF将DataTable绑定到重复的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆