通过 DataTemplates 和 ContentControl 绑定属性

发布于 2024-12-28 11:23:24 字数 1377 浏览 0 评论 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>

如何绑定这些属性?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你曾走过我的故事 2025-01-04 11:23:24

由于 OperationView 后面的 DataContext 将是 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>

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>
素年丶 2025-01-04 11:23:24

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

Text="{Binding SomeProperty}"

(如果仅指定路径,则绑定是相对于DataContext,请注意,在您链接的答案中,目的是在 DataContext 本身上有一个 TwoWay 绑定,它是一个原始字符串,这无法使用像 {Binding 这样的简单绑定来完成.},需要指定针对实际属性的属性路径)

The DataContext in the UserControl is your model object, so you can directly bind to its properties like this:

Text="{Binding SomeProperty}"

(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 a TwoWay binding on the DataContext 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文