Binding properties through DataTemplates and ContentControl
Asked Answered
B

2

0

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?

Buehler answered 24/1, 2012 at 13:35 Comment(2)
is "regular" binding not working? what have you tried?Hammering
I'm fairly new to WPF and I just don't know how to bind using this approach. Could you understand the whole example? Was it clear?Buehler
S
2

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>
Sale answered 24/1, 2012 at 13:48 Comment(8)
Oh god. It worked. I think I was making the things harder to me. I tried many ways, but I didn't tried the simplest one. Thanks =DBuehler
@DiegoStiehl lol that's why I was so confused about your comments in your other question :) Glad you got it workingSale
That's because I'm new to all WPF features. They work in a different way and I think all the beginners have some difficulties with it.Buehler
@DiegoStiehl Yes, it has a learning curve however once you get used to it it's so nice and easy to use :)Sale
And I think You'll soon see a new question from me. I still I have a lot questions, but I think I'm making some progress.Buehler
Isn't there a possibility of use this DataTemplates in a specific view (or another specific method)? I'm afraid It'd appear in a wrong moment (like trying to show an Operation in a different situation).Buehler
@DiegoStiehl Yes. If you want to use the DataTemplate for your entire application, put it in Window.Resources. If you only want to use it in a specific part of your application, put it in that UI Element's Resources, such as ContentControl.Resources or UserControl.Resources. Personally I leave it in the application's resources unless I know there is more than one DataTemplate for the same objectSale
But remember I'm using Prism and the DataTemplates are automatically loaded into the shell project during the modules loading (and if only the respective module is available). I used this technique for loading the DataTemplates. But, well, if you recommend this global declaration, I will use it and hope that it would never give me problems. I'm afraid because I'm exposing a Model instead a ViewModel object.Buehler
U
1

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)

Uruguay answered 24/1, 2012 at 13:49 Comment(5)
Nooo my nemesis is back! I don't think I saw you yesterday :)Sale
@Rachel: Hello there, nemesis, as you can see from my rep graph i am not very active, and it will probably drop even more as the exam season is starting for me right now.Uruguay
Oh good, maybe I can answer some questions :) Actually, I've been trying to limit how much time I spend on here during work hours... its just so easy to pull up the unanswered page while something is building or some other long process is occurring! Good luck on your exams though, not that you need it.Sale
@Rachel: If the exams were on WPF surely i wouldn't need it but sadly that is not the case, so thanks :PUruguay
stackoverflow: a place to meet old friends. hahaBuehler

© 2022 - 2024 — McMap. All rights reserved.