Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

wpf - Binding properties through DataTemplates and ContentControl

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...