无法通过x:Bind将带有Textblock的anObject成员绑定

时间:2018-05-25 14:45:39

标签: xaml uwp uwp-xaml

使用TextBlock绑定的obl.name。 XAML中的代码

<StackPanel Background="Gray" Orientation="Vertical">
<TextBlock Text="{x:Bind obl.name, Mode=TwoWay}" Foreground="Aquamarine" />
<Button Content="click" Click="Button_Click"/>
</StackPanel>

.Cs文件中的代码

public MainPage()
    {
        this.InitializeComponent();
        obl = new Class1();
        DataContext = obl;
    }
    public Class1 obl { get; set; }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        obl.name = "haiiii";
    }

class1

public class Class1
{
    public string name { get; set; }
}

在UI中不显示二进制值。为什么?

1 个答案:

答案 0 :(得分:0)

如果要绑定属性中的某些文本,class1必须实现INotifyPropertyChanged接口并设置属性已更改。

public class Class1 : INotifyPropertyChanged
{
    private string _name;

    public string name
    {
        get => _name;
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

但是作为属性,属性name应该被命名为PascalCasing的Name

Thx @thezapper