简体   繁体   English

WP8 App中无法绑定字符串属性

[英]Can't bind string property in WP8 App

I'd like to implement an automatic updating Binding in my WP8 App. 我想在我的WP8应用程序中实现自动更新绑定。 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. 我想要的只是将类的String属性绑定到文本块,但是只要我没有类就不直接绑定字符串,它就无法工作。

Textblock: 文本块:

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

Definition of MARK1: 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类。 _mark represents PersonName. _mark代表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); 它要简单得多,并且您不需要使用ElementName (除非没有其他选择,否则通常不推荐使用)。 instead get the DataContext directly by setting it in the constructor for example. 而是直接通过在构造函数中进行设置来直接获取DataContext。

If you do use ElementName , what that means is that you're trying to get a property from the named element. 如果确实使用ElementName ,则意味着您正在尝试从命名元素获取属性。 In this case the Page . 在这种情况下, 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. 如果您将MARK作为类的实例属性而不是static ,并假设PhoneApplicationPage实例的x:Name="Page"x:Name="Page" ,则您的代码应该可以使用。 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: 然后,在正确创建DataContext之后,您可以直接引用该属性:

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

Or, use the shortcut syntax where Path is assumed: 或者,使用假定Path的快捷语法:

<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. 然后,您可以创建一个名为MARK的类,并将要公开的用于绑定的属性添加为该类的一部分。 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. 您可以通过使用INotifyPropertyChanged接口来实现。

// 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. 如果未在xaml中设置数据上下文,则在代码隐藏中设置它。

this.DataContext = this;

Hope this helps.... 希望这可以帮助....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM