imslavko
imslavko

Reputation: 6676

XAML Binding to property

I have check box in my XAML+C# Windows Store application. Also I have bool property: WindowsStoreTestApp.SessionData.RememberUser which is public and static.

I want check box's property IsChecked to be consistent (or binded, or mapped) to this bool property.

I tried this: XAML

<CheckBox x:Name="chbRemember1"  IsChecked="{Binding Mode=TwoWay}"/>

C#

chbRemember1.DataContext = SessionData.RememberUser;

Code for property:

namespace WindowsStoreTestApp
{
    public class SessionData
    {
        public static bool RememberUser { get; set; }
    }
}

But it doesn't seem to work. Can you help me?

Upvotes: 8

Views: 18450

Answers (4)

patrick
patrick

Reputation: 16949

<CheckBox x:Name="chbRemember1"  IsChecked="{Binding Path=RememberUser, Mode=TwoWay}"/>

 

public class SessionData : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }


    bool _rememberUser;
    public bool RememberUser
    {
        get
        {
            return _rememberUser;
        }
        set
        {
            _rememberUser = value;
            NotifyPropertyChanged("RememberUser");
        }
    }
}

Upvotes: 5

Dean Kuga
Dean Kuga

Reputation: 12119

You need to implement some form of change notification in order for your check box to be "aware" of any changes to the property. The best bet is to use one of the many MVVM frameworks out there, if not, implement INotifyPropertyChanged in your ViewModel.

Also, typically in WPF, we do not set the DataContext of individual controls but set the DataContext of the Window or User Control to a ViewModel...

Here is an example of a property with change notification through one of the MVVM frameworks:

private bool createTrigger;
public bool CreateTrigger
{
    get { return createTrigger; }
    set { createTrigger = value; NotifyPropertyChanged(m => m.CreateTrigger); }
}

As you can see a simple auto-implemented property cannot be used for data binding in WPF...

I'd recommend going through Data Binding Overview over on MSDN...

Upvotes: 4

Johan Larsson
Johan Larsson

Reputation: 17580

Try like this, note that the property is not static but the backing field is:

public class SessionData : INotifyPropertyChanged
{
    private static bool _rememberUser;

    public event PropertyChangedEventHandler PropertyChanged;

    public bool RememberUser
    {
        get { return _rememberUser; }
        set
        {
            _rememberUser = value;
            OnPropertyChanged();
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
this.DataContext = new SessionData();
<CheckBox x:Name="chbRemember1"  IsChecked="{Binding RememberUser, Mode=TwoWay}"/>

Upvotes: 3

Jerry Nixon
Jerry Nixon

Reputation: 31803

You cannot bind to static properties as static properties cannot raise the PropertyChanged event. You will, of course, need INotifyPropertyChanged. But that is not relevant with static properties. You simply cannot bind to static properties. (You can in WPF and Silverlight)

Upvotes: 3

Related Questions