What is the syntax to define view/presenter pairs in XAML?
Question
Why is that when I define a view with data context in this way:
<views:MessageView DataContext="{x:Type presenters:MessagePresenter}"/>
that my MessagePresenter does not run its constructor?
What is the syntax to define view/presenter pairs like this in XAML?
Solution
{x:Type} returns the type of MessagePresenter, not an instance of MessagePresenter.
The following creates an instance of MessagePresenter:
<views:MessageView>
<views:MessageView.DataContext>
<presenters:MessagePresenter/>
</views:MessageView.DataContext>
</views:MessageView>
OTHER TIPS
x:Type
is resolving a Type
instance that represents the type in question (MessagePresenter
in this case). It's not supposed to create an instance of the type.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow