Search code examples
c#xamldata-bindingcode-behind

What is the equivalent of this xaml binding in code behind?


I have a xaml TextBlock bound as follows:

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

How do I access SelectedItem.LastName in C# codebehind? Thanks.

Details:

Data are from an XML file

 <Player>
 <LastName>...</LastName>
 <Age>...</Age>
 </Player>

The combobox binding was simplified for clarity as above in the xaml file.


Solution

  • Looking for this? http://msdn.microsoft.com/en-us/library/ms742863.aspx

    MyData myDataObject = new MyData(DateTime.Now);      
    Binding myBinding = new Binding("MyDataProperty");
    myBinding.Source = myDataObject;
    myText.SetBinding(TextBlock.TextProperty, myBinding);