简体   繁体   中英

WPF's DataContext questions

I asked about how does it work with INotifyPropertyChanged interface( How does WPF INotifyPropertyChanged work? ), and it requires me to connect XAML's DataContext to the INotifyPropertyChanged inherit instances as follows.

MainViewModel model = new MainViewModel();        
this.DataContext = model;

And I also found a recommendation to have a comment for DataContext that each XMAL uses( http://joshsmithonwpf.wordpress.com/2009/10/24/xaml-tip-datacontext-comment/ ).

When I have multiple XAML files, and when I want to link the DataContext to different ViewModel, I guess I need to make the each XAML.CS file to contain this code (model varies for each xaml.cs) : this.DataContext = model; .

  • Is this correct?
  • How can I do the same thing in XAML file?
  • What's the magic behind this DataContext thing? I mean, how does DataContext work?

The DataContext is really one of the main keys to the binding system in WPF. When you design your View (the XAML), you're setting up data bindings, but these are all being done by name (effectively, as a string). The "nearest" DataContext up the visual hierarchy is the object that WPF uses to find the matching property (by name) and wire up the binding.

The suggestion of putting the comment in place is a good one - it helps because the names chosen really depend on the ViewModel ( DataContext ), so a View's XAML file is really tied to a specific type of DataContext .

Note also that there are other approaches available to wire up the DataContext other than setting it in code behind, including using locators, DataTemplates, setting it directly in XAML, etc.

  • Yes that is correct as far as i know, since this is quite repetetive some MVVM frameworks do this linking for you.

  • In XAML:

     <UserControl ... xmlns:vm="clr-namespace:MyApp.ViewModels"> <UserControl.DataContext> <vm:MyViewModel /> </UserControl.DataContext> <!-- ... --> </UserControl> 
  • It enables short bindings where the Path is relative to the DataContext, eg {Binding Name} binds to DataContext.Name . It also is inherited which can be useful.

Please read the Data Binding Overview if you haven't.

1 - The INotifyPropertyChanged interface updates the property changes to the UI,

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}

2- We can use two ways to set the data context to the view , one option is to set the context in code behind file, but this is tightly coupled with view and its not a good approach, i would suggest the below option, its loosly coupled with the view

<UserControl ...
         xmlns:vm="clr-namespace:MyApp.ViewModels">
<UserControl.DataContext>
    <vm:MyViewModel />
</UserControl.DataContext>
<!-- ... -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM