<p>How do you programmatically set a DataContext that specifies the selected item of a list? </p>

<p>More simply, how do you reproduce this type of binding in code?</p>

<pre><code>&lt;StackPanel&gt;
    &lt;ListBox Name="listBox1" /&gt;
    &lt;TextBox Name="textBox1" DataContext="{Binding ElementName=listBox1, Path=SelectedItem}" /&gt;
&lt;/StackPanel&gt;
</code></pre>

You need to set a Name for the textbox so you can refer to it in code.  Then you should just be able to assign an object to the DataContext property.  You can create a data binding programatically like so:
```
```
Binding binding = new Binding();
binding.ElementName = "listBox1";
binding.Path = new PropertyPath("SelectedItem");
binding.Mode = BindingMode.OneWay;
txtMyTextBox.SetBinding(TextBox.TextProperty, binding);
```
```