使用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中不显示二进制值。为什么?
答案 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