通过 DataTemplates 和 ContentControl 绑定属性
我喜欢这个答案,它几乎适合我。
但是,如果我的 DataTemplate
位于外部 ResourceDictionary
中,我该如何实现此目的?
我正在使用 Prism,并通过使用如下文件为每个模块提供 DataTemplates
(用于通用 CRUD 视图):
<ResourceDictionary ... some hidden ns here ... >
<DataTemplate DataType="{x:Type model:Operation}">
<vw:OperationView />
</DataTemplate>
<DataTemplate DataType="{x:Type model:Customer}">
<vw:CustomerView />
</DataTemplate>
</ResourceDictionary>
然后我使用 这个答案将ResourceDictionaries
合并到Shell应用程序中,我有一个默认的CRUD视图,其中包含该代码:
<ContentControl Content="{Binding MyGenericObject}" />
ContentControl
自动拉出正确的视图。它工作正常,但我想知道绑定每个视图中对象的属性。
这是这些视图的示例 (OperationView.xaml):
<UserControl x:Class="TryERP2.Cadastro.View.OperationView"
... some hidden NS ... >
<StackPanel>
<Label Content="Id" />
<TextBox Text="{Binding ????WHAT????}" />
<Label Content="Description" />
<TextBox Text="{Binding ????WHAT????}" />
</StackPanel>
</UserControl>
如何绑定这些属性?
I liked this answer, and it almost fit me.
But, how can I achieve this if my DataTemplate
is in a external ResourceDictionary
?
I'm using Prism and I provide the DataTemplates
(for generic CRUD views) by each module, by using files like this:
<ResourceDictionary ... some hidden ns here ... >
<DataTemplate DataType="{x:Type model:Operation}">
<vw:OperationView />
</DataTemplate>
<DataTemplate DataType="{x:Type model:Customer}">
<vw:CustomerView />
</DataTemplate>
</ResourceDictionary>
Then I use this answer to merge the ResourceDictionaries
into the Shell app and I have a default CRUD view which has that code:
<ContentControl Content="{Binding MyGenericObject}" />
That ContentControl
automatically pull the correct view. It's working fine, but I want to know bind the property of the objects in each view.
That's a sample of these views (OperationView.xaml):
<UserControl x:Class="TryERP2.Cadastro.View.OperationView"
... some hidden NS ... >
<StackPanel>
<Label Content="Id" />
<TextBox Text="{Binding ????WHAT????}" />
<Label Content="Description" />
<TextBox Text="{Binding ????WHAT????}" />
</StackPanel>
</UserControl>
How can I bind these properties?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
OperationView
后面的DataContext
将是Operation
类型的对象,因此您只需绑定到Operation
上的任何属性即可你想要Since the
DataContext
behindOperationView
will be an object of typeOperation
, then you simply bind to whatever property onOperation
you wantUserControl
中的DataContext
是您的模型对象,因此您可以直接绑定到其属性,如下所示:(如果仅指定路径,则绑定是相对于
DataContext
,请注意,在您链接的答案中,目的是在DataContext
本身上有一个TwoWay
绑定,它是一个原始字符串,这无法使用像{Binding 这样的简单绑定来完成.}
,需要指定针对实际属性的属性路径)The
DataContext
in theUserControl
is your model object, so you can directly bind to its properties like this:(If only a path is specified the binding is relative to the
DataContext
, note that in the answer you linked the intention was to have aTwoWay
binding on theDataContext
itself which was a primitive string, this cannot be done using a simple binding like{Binding .}
, a property path targeting an actual property needs to be specified)