简体   繁体   中英

Binding TextBlock to C# code using Source = {Binding} not working

At the very simplest level all I am trying to do is Data Bind TextBlock control (XAML). Am tying to get string from MyString (property defined in C# code behind) as a Text for TextBlock:

DisplayText disp = new DisplayText();
disp.MyString = "Hello";

 public class DisplayText {
       public string MyString {get;set;}
       }

XAML code:

<TextBlock Grid.Column="1" Text="{Binding Path=MyString}" Foreground="Black"/>

But, Its not working:( Am searching for hours but could'nt get this simple thing done. Plz help!

In your XAML you need to define the DataContext.

For example:

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

Also, you'll need to implement INotifyChanged if you want the screen and model to stay in sync.

Have you seen the msdn article about data binding in Store Apps? http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh464965.aspx

The example code shows how to do what you are describing and worked for me.

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