关于 WPF 中数据绑定的一般设计问题
我开始在我的 WPF 项目中使用 Binding,但实际上我对表示端 (XAML) 上的一些事情感到困惑。
所以我想用类别列表填充 TreeView
。 我知道如何为我的类别实例列表编写正确的 HierarchicalDataTemplate
。
<HierarchicalDataTemplate ItemsSource="{Binding Path=ChildrenCategories}" DataType="{x:Type src:Category}">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</HierarchicalDataTemplate>
但现在我不知道从哪里得到这个列表。 我这里有 2 个解决方案:
我有一个
Library Singleton
类 这归还给我权利 树状,那么我需要使用 我的 xaml 中的ObjectDataProvider
会调用 Library.Instance.Categories 方法。 (这意味着控制器必须与 UI 完全分离)。我有一个
Property ListCategories
在我的页面交互逻辑 (OpenUnit.xaml.cs),并绑定 树。
我不确定 xaml.cs 文件的用途,它们的用途是什么? 它通常用于存储属性(并充当控制器)还是仅仅用于 UI 后端(例如从 UI 获取值?)?
如果 xaml.cs 文件用作控制器,我如何将数据绑定到它,我尝试了很多解决方案但没有成功,我唯一的成功是使用静态绑定。
如果您对 WPF 中的 UI 和逻辑绑定有任何评论或建议,我将不胜感激,希望我不会那么困惑。
提前致谢,
鲍里斯
I'm starting to use Binding in my WPF project and I'm actually confused about few things on the presentation side (XAML).
So I want to populate a TreeView
with a List of Categories. I know how to write the right HierarchicalDataTemplate
for my List of Category instances.
<HierarchicalDataTemplate ItemsSource="{Binding Path=ChildrenCategories}" DataType="{x:Type src:Category}">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</HierarchicalDataTemplate>
But what now I don't know is from where to get the list. I have here 2 solutions :
I got a
Library Singleton
class
which return me the right
arborescence, then I need to use anObjectDataProvider
in my xaml which
would call the
Library.Instance.Categories method. (Which means that the controller has to be completely separated from the UI).I got a
Property ListCategories
in my page interactionLogic
(OpenUnit.xaml.cs), and bind the
tree with it.
I'm not sure about the purpose of the xaml.cs files, what are they made for? Is it normally used to store the properties (and act as a controller) or simply to have a back-end for the UI (for example get values from the UI?)?
In case the xaml.cs file is used as a controller, how do I bind my data to it, I've tried many solutions without success,my only success was with the use of static binding.
I would appreciate any comment or recommandation about UI and Logic Binding in WPF, hopefully I will get less confused.
Thanks in advance,
Boris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
读完这篇精彩的文章后,我不再那么困惑了:
http:// msdn.microsoft.com/en-us/magazine/dd419663.aspx
本文介绍模型视图 ViewController 模式以及 WPF 如何集成它。 所以看起来 xaml.cs 文件应该在这里用作 ViewController,并且应该保存属性。
这实际上是有道理的,因为混合视图和数据并不是一个好的做法,我们希望设计师应该有完全独立的工作要做。
对于解决方案 2),如果将数据上下文设置为当前文件也是可能的。
After reading this great article, I got a little bit less confused :
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
The article is about the Model View ViewController pattern, and how WPF integrates it. So it seems that xaml.cs files should be used as the ViewController here, and should hold the properties.
It actually make sense since it's not a good practice to mix the View and the Data, we want the designers should have a completely independant work to do.
Also for the solution 2) it is possible if you set the data context to the current file.