通过DataTemplates和ContentControl绑定属性 [英] Binding properties through DataTemplates and ContentControl
问题描述
我喜欢这个答案,而且几乎适合我。
但是,如果我的 DataTemplate
在外部 ResourceDictionary
?
But, how can I achieve this if my DataTemplate
is in a external ResourceDictionary
?
我正在使用Prism,我为每个模块提供了 DataTemplates
(用于通用CRUD视图)通过使用这样的文件:
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>
然后我使用这个答案将 ResourceDictionaries
合并到Shell应用程序中,我有一个默认的CRUD视图,它具有以下代码:
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}" />
ContentControl
自动拉出正确的视图。它的工作正常,但我想知道在每个视图中绑定对象的属性。
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.
这是这些视图(OperationView.xaml)的示例:
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>
如何绑定这些属性?
推荐答案
由于 DataContext
后面的 OperationView
将是一个类型为操作
,然后您只需绑定到操作
上的任何属性
Since the DataContext
behind OperationView
will be an object of type Operation
, then you simply bind to whatever property on Operation
you want
<!-- DataContext will be model:Operation per your DataTemplate -->
<UserControl x:Class="TryERP2.Cadastro.View.OperationView"
... some hidden NS ... >
<StackPanel>
<Label Content="Id" />
<TextBox Text="{Binding Id}" />
<Label Content="Description" />
<TextBox Text="{Binding Description}" />
</StackPanel>
</UserControl>
这篇关于通过DataTemplates和ContentControl绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!