2016-07-22 17 views
2

Welches ist der richtige Weg in Datacontext-Code zu setzen hinter Einstellung:Order of Datacontext in Code hinter

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

oder

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

?

+2

Beide sind "richtige". Wenn es sich bei der Ansicht jedoch um ein Benutzersteuerelement handelt, sollten Sie dessen DataContext nicht festlegen, da dies verhindern würde, dass ein DataContext vom Fenster oder vom übergeordneten Steuerelement geerbt wird. – Clemens

Antwort

0

Zu beantworten ist: es hängt ab.

Wenn Ihr Bildschirm viele Daten oder komplexe datengesteuerte Visualisierungen enthält, können Sie das Laden verzögern oder staffeln, wodurch die erste Option möglicherweise besser wird. Wenn es sich um ein einfaches datengebundenes Formular handelt, ist die zweite Option möglicherweise vorzuziehen. Oder es kann sogar vernachlässigbar sein. Wie immer, Messung ist der beste Weg, um die Frage unter Ihren spezifischen Bedingungen zu beantworten.

Lassen Sie uns sehen, was passiert, indem Sie die Verfolgung einer Bindung aktivieren.

<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> 

Erste Option - InitializeComponent erste

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

Hier sehen wir, dass während InitializeComponent (wenn die XAML geladen wird) versuchen, die Bindungen zu lösen, aber zu sehen, dass die DataContext null ist, so dass ihre Auswertung ist zurückgestellt.

Debug-Ausgabe:

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' 

Zweite Option - set DataContext erste

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

Hier werden die Bindungen zu bewerten sofort bei der Initialisierung.

Debug-Ausgabe:

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