2009-08-05 9 views
2
XAMLで

、私はタブ項目として、すべての私のプレゼンターを表示しています:私は、各ビューも私が今まで明示的に例えば言わずに、それぞれのプレゼンターのプロパティにアクセスを持っていることに気付きました私のビューはDataContextのないPresenterについてどのように知っていますか?

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

表示します。 DataContext = thisなど

DataContextはどこに設定されていますか?それは魔法のようにDataTemplateで起こりますか? DataContextが設定されている

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

答えて

2

を:ここで

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; 

    } 
} 

が表示され、それはのDataContextがどこかに設定されていることを私に語った正しくHeaderを表示しますか?それは魔法のようにDataTemplateで起こりますか?

はい。 DataTemplateビジュアルツリーは、それが表すオブジェクトをDataContext

関連する問題