Xaml Can';t通过x:Bind将对象的成员与文本块绑定
obl.name与TextBlock绑定。 XAML中的代码Xaml Can';t通过x:Bind将对象的成员与文本块绑定,xaml,uwp,uwp-xaml,Xaml,Uwp,Uwp Xaml,obl.name与TextBlock绑定。 XAML中的代码 <StackPanel Background="Gray" Orientation="Vertical"> <TextBlock Text="{x:Bind obl.name, Mode=TwoWay}" Foreground="Aquamarine" /> <Button Content="click" Click="Button_Click"/> </StackPanel> 第一类
<StackPanel Background="Gray" Orientation="Vertical">
<TextBlock Text="{x:Bind obl.name, Mode=TwoWay}" Foreground="Aquamarine" />
<Button Content="click" Click="Button_Click"/>
</StackPanel>
第一类
public class Class1
{
public string name { get; set; }
}
绑定值不显示在UI中。为什么?如果要在属性中绑定某些文本,则
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
应命名为name
,以用于PASCALCING
Thx@thezapper
Text=“{x:Bind name}”
。XAML怎么可能知道某个方法中局部变量的名称?您已将该对象指定给DataContext。DataContext是绑定查找属性的地方。您告诉一个绑定,“绑定到name
”,它会在DataContext上查找name
。而且您不需要Mode=TwoWay
,因为文本块无法将值写回源。首先,您需要实现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));
}
}