tags:

views:

56

answers:

1

In XAML, I'm displaying all my presenters as tab items:

<TabControl.ContentTemplate>
    <DataTemplate DataType="x:Type views:SmartFormAreaPresenter">
        <views:SmartFormAreaView/>
    </DataTemplate>
</TabControl.ContentTemplate>

I've noticed that each View has access to its respective Presenter's properties even without me ever explicitly saying e.g. View.DataContext = this, etc.

Where is the DataContext being set then? Does it happen magically with the DataTemplate?

public class SmartFormAreaPresenter : PresenterBase
{

    #region ViewModelProperty: Header
    private string _header;
    public string Header
    {
        get
        {
            return _header;
        }

        set
        {
            _header = value;
            OnPropertyChanged("Header");
        }
    }
    #endregion

    public SmartFormAreaPresenter(XElement areaXml)
    {
        Header = areaXml.Attribute("title").Value;

    }
}

Here is the view, it displays Header correctly which tells me that the DataContext is being set somewhere:

<UserControl x:Class="TestApp.Views.SmartFormAreaView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <DockPanel LastChildFill="True">
        <TextBlock Text="{Binding Header}"/>
    </DockPanel>
</UserControl>
+2  A: 

Where is the DataContext being set then? Does it happen magically with the DataTemplate?

Yes. The DataTemplate visual tree receives the object it represents through the DataContext

Thomas Levesque