XAML Binding to property

15,624

Solution 1

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...

Solution 2

<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");
        }
    }
}

Solution 3

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}"/>

Solution 4

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)

Share:
15,624
imslavko
Author by

imslavko

Updated on June 12, 2022

Comments

  • imslavko
    imslavko almost 2 years

    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?

  • imslavko
    imslavko over 11 years
    Thank you for your answer. Is there any simple way to bind one only property? I do not want generate tons of code only for one property, then it is easier just to handle change event.
  • Dean Kuga
    Dean Kuga over 11 years
    It's not at all complicated to set the DataContext of your Window or User Control to a ViewModel class and in that class define a property as illustrated above. If you prefer not to work with an MVVM framework, it's easisest to implement the INotifyPropertyChanged interface on that class in order for your control to be notified of any changes to the property. It's really not that much code after all...