21

Can someone explain me the following XAML line?

DataContext="{Binding RelativeSource={RelativeSource Self}}"

Here the simple example of use.

How to replace that line with a C# code?

gliderkite
  • 8,828
  • 6
  • 44
  • 80

3 Answers3

14

That simply sets the DataContext property equal to the object with the property. The code equivalent would be this.DataContext = this;

Edit

The DataContext property is the object that is used as the context for all the bindings that occur on this object and its child objects. If you don't have a DataContext correctly set to the model you want to bind to, all of your bindings will fail.

Edit2

Here is how to set it in code behind (matching your example):

public partial class ListViewTest : Window
{
    ObservableCollection<GameData> _GameCollection = 
        new ObservableCollection<GameData>();

    public ListViewTest()
    {
        _GameCollection.Add(new GameData { 
          GameName = "World Of Warcraft", 
          Creator = "Blizzard", 
          Publisher = "Blizzard" });
        _GameCollection.Add(new GameData { 
          GameName = "Halo", 
          Creator = "Bungie", 
          Publisher = "Microsoft" });
        _GameCollection.Add(new GameData { 
          GameName = "Gears Of War", 
          Creator = "Epic", 
          Publisher = "Microsoft" });

        InitializeComponent();

        this.DataContext = this;   //important part
    }

    public ObservableCollection<GameData> GameCollection
    { get { return _GameCollection; } }

    private void AddRow_Click(object sender, RoutedEventArgs e)
    {
      _GameCollection.Add(new GameData { 
          GameName = "A New Game", 
          Creator = "A New Creator", 
          Publisher = "A New Publisher" });
    }
}
Dylan Meador
  • 2,381
  • 1
  • 19
  • 32
  • Ok, but why? And why if I remove this line I can't add any new object to my ListView? – gliderkite Sep 14 '12 at 19:28
  • @gliderkite If you are doing the example that you linked, my guess is that the objects are getting correctly added to your `ObservableCollection`, but the binding is failing. – Dylan Meador Sep 14 '12 at 19:33
  • Yes, i want to know why in the example he sets the DataContext in that way, and if there is a way to not use the XAML line. – gliderkite Sep 14 '12 at 19:37
  • 1
    @gliderkite He does it that way because his Window class contains the property he binds to: **GameCollection**. To do this in code, set the `DataContext` property of your `Window` class equal to the instance of your `Window` – Dylan Meador Sep 14 '12 at 19:42
2

It means "The DataContext is the Owner of this DataContext property" thus the control.

In C# it would be

myTextBox.DataContext = myTextBox;
dowhilefor
  • 10,971
  • 3
  • 28
  • 45
  • Doesn't make much sense to me. Shouldn't the self-reference be the default? – The incredible Jan Sep 08 '22 at 13:13
  • 1
    In WPF DataBinding assumes that you want your bindings to target your BusinessModel, which is stored in the DataContext, most of the time. That way you can always directly bind your View to your Data. In some other cases you actually want to refere to other View Objects, or to your own control. In those cases you overwrite the Source to another target, then the default DataContext. – dowhilefor Sep 12 '22 at 11:05
  • The reason for my question was that I "inherited" a project without much documentation and sometimes I get warnings in XAML editor about missing DataContext but there's no problem at runtime. Most of the time the window's constructor calls "LayoutRoot.DataContext = this;" and the bindings use properties from it's CodeBehind. I'm slowly getting behind it, I think. :) – The incredible Jan Sep 19 '22 at 10:56
0

To answer to your second question: Sometime can be useful to declare DataContext on XAML because you can see databinding at design time. If you declare it by code, the databinding will be done only at runtime.

There are other ways to achieve design time (fake) data. To learn more, please query about "bendability".

Note: As a general rule, please remember that if you have another question, you should create a new stackoverflow request :-)