Xamarin.Forms刷新Editor的TextProperty

时间:2014-09-23 00:20:32

标签: data-binding inotifypropertychanged xamarin.forms

有没有办法在事件后更改编辑器单元格中的文本?

我有一个编辑器单元格,显示SQLite数据库中的地址。我还有一个获取当前地址的按钮,并在警告中显示此信息,询问他们是否要将地址更新为此。如果是,那么我想在编辑器单元格中显示新地址。

public class UserInfo : INotifyPropertyChanged
{
    public string address;
    public string Address 
    { 
        get { return address; }
        set
        {
            if (value.Equals(address, StringComparison.Ordinal))
            {
                 return;
            }
            address = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我的编辑器单元代码是

Editor userAddress = new Editor
{
    BindingContext = uInfo, // have also tried uInfo.Address here
    Text = uInfo.Address,
    Keyboard = Keyboard.Text,

};

然后在得到当前地址之后我有了这个

bool response = await DisplayAlert("Current Address", "Would you like to use this as your address?\n" + currAddress, "No", "Yes");
   if (response)
   {
        //we will update the editor to show the current address
        uInfo.Address = currAddress;
   }

如何让它更新编辑器单元格以显示新地址?

1 个答案:

答案 0 :(得分:2)

您正在设置控件的BindingContext,但未指定与其一起使用的绑定。您希望将编辑器的TextProperty绑定到上下文的Address属性。

Editor userAddress = new Editor
{
    BindingContext = uinfo,
    Keyboard = Keyboard.Text
};

// bind the TextProperty of the Editor to the Address property of your context
userAddress.SetBinding (Editor.TextProperty, "Address");

这也可行,但我不肯定语法是正确的:

Editor userAddress = new Editor
{
    BindingContext = uinfo,
    Text = new Binding("Address"),
    Keyboard = Keyboard.Text
};