使用 MVVM 和 WCF 的 WPF TreeView 绑定
我目前正在使用 MVVM 模式开发 WPF 应用程序。 View、Model、ViewModel 各自位于单独的类库中。
我的视图有一个 TreeView 控件,应使用数据绑定填充。
我从通过模型访问的 WCF 服务获取数据。我的模型具有对 WCF 服务的服务引用,并调用 WCF 中的方法来检索嵌套集合。
ViewModel 使数据可供视图使用。检索数据等一切正常,除了在 TreeView 中显示之外。
我的 TreeView 的 XAML 看起来像这样
<TreeView Grid.Row="1" ItemsSource="{Binding CustomerTree}" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type ....}" ItemsSource="{Binding Path=Children}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type ....}" ItemsSource="{Binding Path=Children}">
<TextBlock Text="{Binding Path=ID}"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
DataType="{x:Type .....} 属性需要某种类型,该类型应该是我的嵌套集合中的类型之一,但视图不知道哪些类型可用。 ViewModel 可以使用 Model.ServiceReference.Customer 访问服务引用的类型,
该模型是添加服务引用的正确位置吗?
我的 ,
授予
I'm currently developing a WPF application using the MVVM pattern. The View, Model, ViewModel each are in a seperate Class Library.
My View has a TreeView control which should be filled using databinding.
I get my data from a WCF service which is accessed through the Model. My Model has a service references to the WCF service and calls a method from the WCF to retrieve a nested collection.
The ViewModel makes the data available to the View. Retrieving the data etc all works fine, except for showing it in the TreeView.
My XAML for the TreeView looks like this
<TreeView Grid.Row="1" ItemsSource="{Binding CustomerTree}" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type ....}" ItemsSource="{Binding Path=Children}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type ....}" ItemsSource="{Binding Path=Children}">
<TextBlock Text="{Binding Path=ID}"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
The DataType="{x:Type .....} property expects a certain Type which should be one of the types in my nested collection, but the view does not know which types are available. My ViewModel can access the types the service reference has in the model, for example the customer type by using Model.ServiceReference.Customer.
Is the Model the right place to add a service reference? How should I make my TreeView display the data?
Thanks,
Grant
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将不得不给自己带来一些不便才能达到所需的抽象级别。此问题的解决方案需要执行多个步骤:
DataType
(而是使用x:Key
)。这就是不便的根源:模板将不再自动应用。但从好的方面来说,您刚刚摆脱了对特定类型名称的引用。TreeView
上的ItemTemplate
或ItemTemplateSelector
属性手动选择每个项目的HierarchicalDataTemplate
。对于第二步,这两种技术的描述和示例由我对另一个问题的回答提供。问题。
最终结果是,您将能够选择所需的模板,而无需在 XAML 中引用特定类型,而是利用数据绑定对象上的属性。
You will have to inconvenience yourself a bit to get at the desired level of abstraction. The solution to this issue requires a number of steps:
DataType
for your templates (usex:Key
instead). This is the source of the inconvenience: the templates won't be applied automatically anymore. But on the plus side, you just got rid of references to specific type names.ItemTemplate
orItemTemplateSelector
property on theTreeView
to manually select theHierarchicalDataTemplate
for each item.For the second step, descriptions and examples for both techniques are provided by my answer to another question.
The end result is that you will be able to select the desired template without needing to reference specific types in XAML but by utilizing a property on the databound object instead.