WPF:与非静态参数绑定? (新手问题)
这可能是显而易见的,但我找不到最好的方法。
我想在列表框中显示用户的待办事项。这些 ToDo 位于数据库中,由 Id、UserId 和 Description 组成。
用户登录到该应用程序。
如何检索该特定用户 ID 的待办事项并将其设置为绑定到列表框?
我正在尝试使用 ObjectDataProvider,但我不知道如何将其与非静态内容(例如我的 _dbService、userId、语言等)结合使用。
使所有这些东西静态化与在后面的代码中绑定的唯一选择是什么?
如果是这样,这意味着 ObjectDataProvider 不是很有用,不是吗? 我发现很多它与硬编码参数一起使用的例子,但我几乎没有看到任何需要这样的功能的情况。
This will probably be obvious but I can't find the best way.
I want to show the user's ToDo's in a listbox. These ToDo's are in the database and consist of an Id, UserId and Description.
The user logged in to the app.
How can I retrieve the ToDo's for that certain userId and set it up for binding to the listbox?
I was trying with an ObjectDataProvider but I cant figure out how to use that in combination with nonstatic stuff (like my _dbService, userId, language, ...).
Are the only options to make all those things static versus binding in the code behind?
If so, this means that ObjectDataProvider isn't very useful, no?
I find a lot of examples of it being used with a hardcoded parameter but I hardly see any situation where I'd need such a functionality..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用 Model-View-ViewModel 模式完成所有 WPF。我已经给了你一个链接,但谷歌会给你很多。 MVVM 似乎是 WPF 的标准模式。 这个项目可能比您需要的更复杂,但它写得很好并且熟悉 MVVM 的使用。
基本上,您创建数据模型。在本例中,您可能会创建一个带有属性 Id、UserID 和 Description 的简单类(我将其称为 ToDoItem)。使用您喜欢的机制从数据库中获取这些内容的集合。链接到 SQL、实体框架、标准查询等等。
然后你就有了 ViewModel - 每个模型实例都有一个 ViewModel 实例:VM 有一个对 M 的引用并“转发”它的属性。 ViewModel 是用来操作模型的。
然后你就有了你的视图——这就是用户界面。您将视图的 DataContext 设置为 ViewModel,然后您的绑定会自动绑定到 ViewModel。你的视图最终只是你能看到的东西。所有工作都在 ViewModel 中完成。这意味着它很容易测试。
因此,当您单击视图中的按钮时,绑定会将其传递到 ViewModel 中操作模型的命令。
UI 也是一个具有 ViewModel 的视图。因此,您的 UI VM 可能会从数据库加载模型集合并将它们粘贴到 ObservableCollection 中。 ListBox 项目集合将绑定到此 ObservableCollection。
很难在这样的帖子中解释所有这些。阅读几篇文章,看看您的想法。我对此也还很陌生,但我相信我对 MVVM 的阅读已经得到了回报。
I do all my WPF using the Model-View-ViewModel pattern. I've given you one link there but Google will give you loads. MVVM seems to be the standard pattern for WPF. This project is probably more complicated than you need but it is well-written and brings home the use of MVVM.
Basically, you create a Model of your data. In this case, you'd probably create a simple class (I'll call it ToDoItem) with properties Id, UserID and Description. Use your preferred mechanism to get a collection of these from the database. Link to SQL, Entity Framework, a standard query, whatever.
Then you have your ViewModel - you have an instance of the ViewModel for each instance of the Model: the VM has a reference to the M and 'forwards' properties to it. The ViewModel is what you use to manipulate the model.
Then you have your View - this is the UI. You set the DataContext of the View to be the ViewModel and then your bindings automatically bind to the ViewModel. Your View just ends up being the things you can see. All of the work gets done in the ViewModel. This means it's very easy to test.
So, when you click on a button in your View, the bindings pass this onto a Command in your ViewModel which manipulates the Model.
The UI is also a View with a ViewModel. So, your UI VM might load a collection of Models from the database and stick them in an ObservableCollection. The ListBox items collection would be bound to this ObservableCollection.
It's hard to explain all of this in a post like this. Read a couple of articles and see what you think. I'm still quite new at this, too, but I believe my reading about MVVM has paid off.
Hela Thomas、Tom 来自 Orbit One :)
MVVM 是最佳选择。我正在进行我的第四个项目,如果您使用 mvvm,WPF 确实会大放异彩。您已经尝试过 MVC(或 MVP,就像我们在 recy*tyre 上所做的那样),这是一个很好的关注点分离。
MVVM 更进一步,因为视图模型对视图一无所知。
视图绑定到视图模型,因此它有对其的引用(2 种方式,超级强大,并且超出了典型的 MS 演示)。 viewmodel 只是一个 poco,是你的视图、数据+行为的表示。一旦你深入了解了这一段,mvvm这个很酷的术语就没有秘密了。
我看看是否可以拿出一个小演示。也许稍后我会有时间。
我将提出的是一个绑定到视图模型(文件 2,一个 poco 类,不要与后面的代码混淆)的视图(xaml,文件 1)。该模型可以是您喜欢的任何模型(服务层或直接到存储库)。使用 2 路绑定的力量,我们将绑定到一个可观察的集合,这意味着如果我们向集合添加/删除/...某些内容,视图将拾取它,而无需我们投入精力。
我的前 2 个 wpf 项目是使用 Caliburn Micro 完成的(请参阅 codeplex),这是一个基于约定的强大框架。它使您远离硬核 wpf(主要是自己创建游览依赖属性),并且您可以在不完全理解 wpf 的情况下相对快速地创建一些东西。这本身就是一个缺点,但它对我有用。从项目 3 开始,我开始自己驯服这些依赖属性,这将使您成为更好的 wpf 开发人员。
我看到问题是从十月开始的..你找到好的解决方案了吗?
Hela Thomas, Tom here from Orbit One :)
MVVM is the way to go. I'm on my 4th project and WPF really shines if you use mvvm. You already tried MVC (or MVP as we did on recy*tyre) and that's a nice separation of concern.
MVVM takes it a step further since the viewmodel knows absolutely nothing about the view.
The view binds to the viewmodel, so it has a reference to it (2 way, super powerful and works beyond the typical MS demo). The viewmodel is just a poco and is a representation of your view, data + behaviour. Once you dig this paragraph the cool term mvvm will have no secrets.
I see if I can come up with a small demo. Maybe I'll have time later.
What I will come up with is a view (xaml, file 1) that binds to a viewmodel (file 2, a poco class, not to be mistaken with code behind). The model can be whatever you like (service layer or directly to the repositories). Using the power of 2 way binding we will bind to an observable collection meaning that if we add/delete/... something to the collection the view will pick it up without us putting energy into it.
My first 2 wpf projects was done with Caliburn Micro (see codeplex) which is a powerful framework based on conventions. It shields you away from hardcore wpf (creating tour dependency properties yourself mainly) and you can create something relatively fast without fully understanding wpf. That's a downside of itself but it worked for me. As of project 3 I started taming those dependency properties myself and it will make you a better wpf developer.
I see the question is from October.. did you find a good solution?