2016-07-22 73 views
2

,哪一項是設置的DataContext在後面的代碼以正確的方式:訂購代碼設置DataContext的背後

public ViewConstructor() 
{ 
    InitializeComponent(); 
    DataContext = new MyViewModel(); 
} 

public ViewConstructor() 
{ 
    DataContext = new MyViewModel(); 
    InitializeComponent(); 
} 

+2

兩者都是 「正確的」。但是,如果視圖是一個用戶控件,你不應該設置它的DataContext可言,因爲這將阻止繼承從窗口或父控件一個DataContext。 – Clemens

回答

0

給的答覆是:這取決於

如果你的屏幕中含有大量數據或複雜數據驅動的可視化的,你可能要推遲或錯開其負荷,這可能使第一個選項更好。如果它是一個簡單的數據綁定表單,則第二個選項可能更可取。或者它甚至可以忽略不計。與往常一樣,測量是回答您的具體條件下問題的最佳途徑。

讓我們來看看通過啓用跟蹤上一個綁定會發生什麼。

<Window x:Class="WpfApplication9.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid IsEnabled="{Binding IsEnabled, PresentationTraceSources.TraceLevel=High}" /> 
</Window> 

第一種選擇 - InitializeComponent第一

public MainWindow() 
{ 
    Debug.WriteLine("Initializing"); 
    InitializeComponent(); 
    Debug.WriteLine("Initialized"); 
    Debug.WriteLine("Setting DataContext"); 
    DataContext = new ViewModel(); 
    Debug.WriteLine("DataContext Set"); 
} 

在這裏,我們看到,在InitializeComponent(當XAML加載)的綁定嘗試解決,但看到DataContext爲空,所以他們的評價是推遲

調試輸出:

Initializing 
System.Windows.Data Warning: 56 : Created BindingExpression (hash=55924514) for Binding (hash=26055869) 
System.Windows.Data Warning: 58 : Path: 'IsEnabled' 
System.Windows.Data Warning: 60 : BindingExpression (hash=55924514): Default mode resolved to OneWay 
System.Windows.Data Warning: 61 : BindingExpression (hash=55924514): Default update trigger resolved to PropertyChanged 
System.Windows.Data Warning: 62 : BindingExpression (hash=55924514): Attach to System.Windows.Controls.Grid.IsEnabled (hash=21411931) 
System.Windows.Data Warning: 67 : BindingExpression (hash=55924514): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=55924514): Found data context element: Grid (hash=21411931) (OK) 
System.Windows.Data Warning: 71 : BindingExpression (hash=55924514): DataContext is null 
System.Windows.Data Warning: 65 : BindingExpression (hash=55924514): Resolve source deferred 
Initialized 
Setting DataContext 
DataContext Set 
System.Windows.Data Warning: 67 : BindingExpression (hash=55924514): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=55924514): Found data context element: Grid (hash=21411931) (OK) 
System.Windows.Data Warning: 78 : BindingExpression (hash=55924514): Activate with root item ViewModel (hash=45063479) 
System.Windows.Data Warning: 108 : BindingExpression (hash=55924514): At level 0 - for ViewModel.IsEnabled found accessor ReflectPropertyDescriptor(IsEnabled) 
System.Windows.Data Warning: 104 : BindingExpression (hash=55924514): Replace item at level 0 with ViewModel (hash=45063479), using accessor ReflectPropertyDescriptor(IsEnabled) 
System.Windows.Data Warning: 101 : BindingExpression (hash=55924514): GetValue at level 0 from ViewModel (hash=45063479) using ReflectPropertyDescriptor(IsEnabled): 'False' 
System.Windows.Data Warning: 80 : BindingExpression (hash=55924514): TransferValue - got raw value 'False' 
System.Windows.Data Warning: 89 : BindingExpression (hash=55924514): TransferValue - using final value 'False' 

第二個選項 - 設置DataContext第一

public MainWindow() 
{ 
    Debug.WriteLine("Setting DataContext"); 
    DataContext = new ViewModel(); 
    Debug.WriteLine("DataContext Set"); 
    Debug.WriteLine("Initializing"); 
    InitializeComponent(); 
    Debug.WriteLine("Initialized"); 
} 

這裏的綁定初始化時立即評估。

調試輸出:

Setting DataContext 
DataContext Set 
Initializing 
System.Windows.Data Warning: 56 : Created BindingExpression (hash=27331439) for Binding (hash=41386841) 
System.Windows.Data Warning: 58 : Path: 'IsEnabled' 
System.Windows.Data Warning: 60 : BindingExpression (hash=27331439): Default mode resolved to OneWay 
System.Windows.Data Warning: 61 : BindingExpression (hash=27331439): Default update trigger resolved to PropertyChanged 
System.Windows.Data Warning: 62 : BindingExpression (hash=27331439): Attach to System.Windows.Controls.Grid.IsEnabled (hash=16919637) 
System.Windows.Data Warning: 67 : BindingExpression (hash=27331439): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=27331439): Found data context element: Grid (hash=16919637) (OK) 
System.Windows.Data Warning: 78 : BindingExpression (hash=27331439): Activate with root item ViewModel (hash=25445597) 
System.Windows.Data Warning: 108 : BindingExpression (hash=27331439): At level 0 - for ViewModel.IsEnabled found accessor ReflectPropertyDescriptor(IsEnabled) 
System.Windows.Data Warning: 104 : BindingExpression (hash=27331439): Replace item at level 0 with ViewModel (hash=25445597), using accessor ReflectPropertyDescriptor(IsEnabled) 
System.Windows.Data Warning: 101 : BindingExpression (hash=27331439): GetValue at level 0 from ViewModel (hash=25445597) using ReflectPropertyDescriptor(IsEnabled): 'False' 
System.Windows.Data Warning: 80 : BindingExpression (hash=27331439): TransferValue - got raw value 'False' 
System.Windows.Data Warning: 89 : BindingExpression (hash=27331439): TransferValue - using final value 'False' 
Initialized