简体   繁体   中英

Can't bind string property in WP8 App

I'd like to implement an automatic updating Binding in my WP8 App. All i want is to bind a String property of a class to a textblock but it doesn't work as long I'm not binding the string direct without the class.

Textblock:

<Textblock Text="{Binding Path=MARK1._mark, ElementName=Page, Mode=OneWay}"/>

Definition of MARK1:

public partial class MainPage : PhoneApplicationPage
{
public static MARK MARK1 = new MARK("example")
public MainPage(){} //constructor
}

I implemented the class MARK like this example in the same namespace as the rest. _mark represents PersonName.

If someone could give me some advice on how to make it work I'd be really Thankfull.

EDIT: I now tried everything suggested in this post, but it's still not working. Maybe this helps someone identifying the problem, when I bind a string that isn't in a class, it works.

It's much simpler, and you shouldn't need to use ElementName (and normally wouldn't recommend it unless no other options work); instead get the DataContext directly by setting it in the constructor for example.

If you do use ElementName , what that means is that you're trying to get a property from the named element. In this case the Page . If you'd added MARK as an instance property of the class rather than a static , and assuming that the PhoneApplicationPage instance was named x:Name="Page" , your code should work. Notifications may not have worked though if the value changed, as shown below.

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        this.DataContext = new MARK()
        {
            Name = "UserName"
        };

    }

Then, with the DataContext properly created, you can just refer to the property directly:

<Textblock Text="{Binding Path=Name}"/>

Or, use the shortcut syntax where Path is assumed:

<Textblock Text="{Binding Name}"/>

Then, you could create a class called MARK , and add the properties you want to expose for binding as part of the class. When a property value changes, you need to raise an event that the property value has changed. You do that by using the INotifyPropertyChanged interface.

// others, plus....
using System.ComponentModel;
using System.Runtime.CompilerServices;

public class MARK : INotifyPropertyChanged
{
    private string _name;
    public string Name {
        get { return _name; }
        set {
            if (_name != value)
            {
                _name = value;
                RaisePropertyChanged();
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    // by using the CallerMemberName attribute, you don't need to specify
    // the name of the property, the compiler provides it automatically
    private void RaisePropertyChanged([CallerMemberName] string propName = "")
    {
        if (string.IsNullOrWhiteSpace(propName)) { 
            throw new ArgumentNullException("propName"); 
        }
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

How are you setting the datacontext.

First set the datacontext. If you are not setting the datacontext in xaml then set it in codebehind.

this.DataContext = this;

Hope this helps....

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