none
Setting DataContext to "this" in xaml

    Question

  • I have a Window1.xaml.cs and a Window1.xaml file.

    In the constructor in a .cs file, after InitializeComponent(), I usually set the DataContext of xaml elements to "this".

    For example, I have a grid called "gridTesting" and I set the DataContext like so: gridTesting.DataContext = this. Then in the xaml I will have access to all the properties in Window1 and can set binding on the grid with something like this:

    Visibility="{Binding TestProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

    ..where TestProperty is a boolean property in Window1.

    How can I set the DataContext to "this" inside of a xaml file though?
    Tuesday, January 05, 2010 10:18 PM

Answers

  • Hi there,

    FWIW - an alternative that works without naming the window:
    DataContext="{Binding RelativeSource={RelativeSource Self}}"


    Cheers,
    Olaf
    • Marked as answer by Linda Liu Wednesday, January 06, 2010 9:33 AM
    Wednesday, January 06, 2010 9:12 AM
  • You want to name your window and then set the DataContext={Binding ElementName=WINDOW_NAME}.  See below:
    <Window 
      x:Name="_this"
      x:Class="WpfApplication6.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"
      DataContext="{Binding ElementName=_this}">
        <Grid>
          <TextBlock Text="{Binding Name}" />
        </Grid>
    </Window>
    
    Tuesday, January 05, 2010 11:23 PM
  • Hi BBauer,

    In addition, you should initialize the value of the property in the Window before the line of code "InitializeComponent()" is called, so as to get the value of the property to show in the Window.  For example:

    markup:

    <Window x:Name="_this" x:Class="WpfApplication1.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"
      DataContext="{Binding ElementName=_this}">
        <Grid>
          <TextBlock Text="{Binding Name}" />
        </Grid>
    </Window>

    code behind:
     public partial class MainWindow : Window {
           
            public Main() { 
               MyText = "some text";
               InitializeComponent();
            }
          
            public string MyText { get; set; }
        }

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Wednesday, January 06, 2010 9:33 AM
    Wednesday, January 06, 2010 8:54 AM

All replies

  • You want to name your window and then set the DataContext={Binding ElementName=WINDOW_NAME}.  See below:
    <Window 
      x:Name="_this"
      x:Class="WpfApplication6.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"
      DataContext="{Binding ElementName=_this}">
        <Grid>
          <TextBlock Text="{Binding Name}" />
        </Grid>
    </Window>
    
    Tuesday, January 05, 2010 11:23 PM
  • Hi BBauer,

    In addition, you should initialize the value of the property in the Window before the line of code "InitializeComponent()" is called, so as to get the value of the property to show in the Window.  For example:

    markup:

    <Window x:Name="_this" x:Class="WpfApplication1.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"
      DataContext="{Binding ElementName=_this}">
        <Grid>
          <TextBlock Text="{Binding Name}" />
        </Grid>
    </Window>

    code behind:
     public partial class MainWindow : Window {
           
            public Main() { 
               MyText = "some text";
               InitializeComponent();
            }
          
            public string MyText { get; set; }
        }

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Wednesday, January 06, 2010 9:33 AM
    Wednesday, January 06, 2010 8:54 AM
  • Hi there,

    FWIW - an alternative that works without naming the window:
    DataContext="{Binding RelativeSource={RelativeSource Self}}"


    Cheers,
    Olaf
    • Marked as answer by Linda Liu Wednesday, January 06, 2010 9:33 AM
    Wednesday, January 06, 2010 9:12 AM
  • Thanks all.

    Linda, in your sample this line:

    <TextBlock Text="{Binding Name}" />

    Should really be this right:

    <TextBlock Text="{Binding MyText}" />

    Thanks.
    Wednesday, January 06, 2010 12:43 PM
  • public Thing()
    {
       this.InitializeComponent();
       this.Loaded += new RoutedEventHandler(Thing_Loaded);
    }
    
    void Thing_Loaded(object sender, RoutedEventArgs e)
    {
        // queue on the ui thread
        Dispatcher.BeginInvoke(
           new Action(() =>
           {
               this.LayoutRoot.DataContext = this;
           }));
    }
    I've had issues setting the datacontext in xaml to the current instance, although if you use the databinding wizard in blend it goes ahead and does it anyway. I have had no problems when setting it in code as above.
    Wednesday, April 25, 2012 11:48 AM