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?

Community
  • 1
  • 1
Diego Stiehl
  • 403
  • 8
  • 25

2 Answers2

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>
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • 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 =D – Diego Stiehl Jan 24 '12 at 13:56
  • @DiegoStiehl lol that's why I was so confused about your comments in your other question :) Glad you got it working – Rachel Jan 24 '12 at 13:58
  • 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. – Diego Stiehl Jan 24 '12 at 14:01
  • @DiegoStiehl Yes, it has a learning curve however once you get used to it it's so nice and easy to use :) – Rachel Jan 24 '12 at 14:03
  • 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. – Diego Stiehl Jan 24 '12 at 14:03
  • 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). – Diego Stiehl Jan 24 '12 at 16:10
  • @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 object – Rachel Jan 24 '12 at 16:58
  • 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](http://stackoverflow.com/a/1172794/1148768) 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. – Diego Stiehl Jan 24 '12 at 17:25
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)

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Nooo my nemesis is back! I don't think I saw you yesterday :) – Rachel Jan 24 '12 at 13:57
  • @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. – H.B. Jan 24 '12 at 14:03
  • 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. – Rachel Jan 24 '12 at 14:09
  • @Rachel: If the exams were on WPF surely i wouldn't need it but sadly that is not the case, so thanks :P – H.B. Jan 24 '12 at 14:14
  • stackoverflow: a place to meet old friends. haha – Diego Stiehl Jan 24 '12 at 16:16