0

我喜欢这个答案,它几乎适合我。

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>

如何绑定这些属性?

4

2 回答 2

2

由于DataContext后面OperationView将是类型的对象Operation,因此您只需绑定到Operation您想要的任何属性

<!-- 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>
于 2012-01-24T13:48:41.600 回答
1

DataContextin是您的UserControl模型对象,因此您可以直接绑定到它的属性,如下所示:

Text="{Binding SomeProperty}"

(如果只指定了一个路径,则绑定是DataContextTwoWay对于需要指定以实际属性为目标)DataContext{Binding .}

于 2012-01-24T13:49:32.397 回答