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?

2
  • is "regular" binding not working? what have you tried? Commented Jan 24, 2012 at 13:40
  • 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? Commented Jan 24, 2012 at 13:47

2 Answers 2

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> 
Sign up to request clarification or add additional context in comments.

8 Comments

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
@DiegoStiehl lol that's why I was so confused about your comments in your other question :) Glad you got it working
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.
@DiegoStiehl Yes, it has a learning curve however once you get used to it it's so nice and easy to use :)
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.
|
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)

5 Comments

Nooo my nemesis is back! I don't think I saw you yesterday :)
@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.
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: If the exams were on WPF surely i wouldn't need it but sadly that is not the case, so thanks :P
stackoverflow: a place to meet old friends. haha

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.