Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML data binding to a global variable?

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

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 a Textboxes text to "is_verifying".

Thanks.

like image 673
Travv92 Avatar asked Nov 21 '12 14:11

Travv92


People also ask

How do I bind data in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

What is DataBinding in WPF?

Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.

How do you bind data?

Select the first TextBox. Expand "Other data source" ---> "Project data source" --> "AdventureWorksdataset" --> "Employee" --> "EmloyeeId". Select the second TextBox. Expand the DataBinding property then select "Text" ---> "Employee Binding source" then select column(National ID) from the list.

Which class is used for data binding in WPF?

WPF data binding supports data in the form of CLR objects and XML. To provide some examples, your binding source may be a UIElement, any list object, a CLR object that is associated with ADO.NET data or Web Services, or an XmlNode that contains your XML data.


2 Answers

First off you can only bind to properties, so you need to add a getter and setter.

public static bool is_verifying { get; set; }

Next you can either set the DataContext of your form to be your class here, and bind with a simple:

"{Binding is_verifying}"

Or create a reference to your class in the resources of the form and reference it like so:

<Window.Resources>
    <local:Login x:Key="LoginForm"/>
</Window.Resources>
...

<TextBox Text="{Binding Source={StaticResource LoginForm}, Path=is_verifying}"/>
like image 169
Kevin DiTraglia Avatar answered Oct 10 '22 00:10

Kevin DiTraglia


You can't bind to a field, you'll need to make it a Property, and still, then you won't be notified of changes unless you implement some kind of notification mechanism, which can be achieved e.g. by implementing INotifyPropertyChanged or by making the property a DependencyProperty.

When 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, which might not work in your case since they require either creating a dummy instance of your class or making it a singleton. Also i think at least in Windows phone 7 x:Static is not available. So you might want to consider making the property an instance property, maybe on a separate ViewModel which you can then set as a DataContext.

like image 34
Botz3000 Avatar answered Oct 10 '22 00:10

Botz3000