Binding XAML data to a global variable?

How can I bind TextBoxes Text to a global variable in my class in XAML? This, by the way, is for Windows Phone.

Here is the code:

namespace Class { public partial class Login : PhoneApplicationPage { public static bool is_verifying = false; public Login() { InitializeComponent(); } private void login_button_Click(object sender, RoutedEventArgs e) { //navigate to main page NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute)); } private void show_help(object sender, EventArgs e) { is_verifying = true; } } } 

And I want to bind the text of the text fields to "is_verifying".

Thanks.

+6
source share
2 answers

At first, you can only bind to properties, so you need to add a getter and setter.

 public static bool is_verifying { get; set; } 

Then you can either set the DataContext your form in your class here, and associate with a simple one:

 "{Binding is_verifying}" 

Or create a link to your class in the form resources and specify it like this:

 <Window.Resources> <local:Login x:Key="LoginForm"/> </Window.Resources> ... <TextBox Text="{Binding Source={StaticResource LoginForm}, Path=is_verifying}"/> 
+13
source

You cannot become attached to the field, you will need to make it a Property, and yet, you will not be notified of changes if you do not implement any notification mechanism that can be achieved, for example. by implementing INotifyPropertyChanged or by creating the DependencyProperty property.

If you have a property, you can usually use the x:Static markup extension to bind to it.

But binding to a static property requires some tricks that may not work in your case, since they require either creating a dummy instance of your class, or creating its singleton. I also think that at least on Windows Phone 7 x:Static not available. Thus, you may want to make the property a property of the instance, perhaps on a separate ViewModel, which you can set as a DataContext .

+1
source

All Articles